DataTable.WriteXML 和子节点

发布于 2024-11-27 17:42:19 字数 1337 浏览 1 评论 0原文

我有以下代码片段:

        var dataSet = new DataSet("Data");
        var table = GetCustomerTable();
        var orgTable = GetOrganizationTable();
        dataSet.Tables.Add(table);
        dataSet.Tables.Add(orgTable);
        var relation = new DataRelation("CustomerMembership", dataSet.Tables["Customers"].Columns["CustomerId"], dataSet.Tables["Organizations"].Columns["CustomerId"])
                        {Nested = true};
        dataSet.Relations.Add(relation);
        dataSet.WriteXml(@"C:\MyFeed.xml");

这给了我以下结果:

<Customers>
    <CustomerId>272408857</CustomerId>
    <snip>
    <Organizations>
      <OrganizationName>Org1</OrganizationName>
    </Organizations>
    <Organizations>
      <OrganizationName>Org2</OrganizationName>
    </Organizations>
  </Customers>

我正在寻找的是这样的:

<Customers>
    <CustomerId>272408857</CustomerId>
    <snip>
    <Organizations>
      <OrganizationName>Org1</OrganizationName>
      <OrganizationName>Org2</OrganizationName>
    </Organizations>
  </Customers>

关于如何将 OrganizationName 嵌套在一个 中的任何想法组织节点;而不是有 2 个节点,每个节点一个值?

I've got the following snippet of code:

        var dataSet = new DataSet("Data");
        var table = GetCustomerTable();
        var orgTable = GetOrganizationTable();
        dataSet.Tables.Add(table);
        dataSet.Tables.Add(orgTable);
        var relation = new DataRelation("CustomerMembership", dataSet.Tables["Customers"].Columns["CustomerId"], dataSet.Tables["Organizations"].Columns["CustomerId"])
                        {Nested = true};
        dataSet.Relations.Add(relation);
        dataSet.WriteXml(@"C:\MyFeed.xml");

Which gives me the following result:

<Customers>
    <CustomerId>272408857</CustomerId>
    <snip>
    <Organizations>
      <OrganizationName>Org1</OrganizationName>
    </Organizations>
    <Organizations>
      <OrganizationName>Org2</OrganizationName>
    </Organizations>
  </Customers>

What I'm looking for is something like this:

<Customers>
    <CustomerId>272408857</CustomerId>
    <snip>
    <Organizations>
      <OrganizationName>Org1</OrganizationName>
      <OrganizationName>Org2</OrganizationName>
    </Organizations>
  </Customers>

Any ideas on how I can get OrganizationName nested inside of one Organization node; instead of having 2 nodes with one value each?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尴尬癌患者 2024-12-04 17:42:19
DataColumn dc = orgTable.Columns["OrganizationName"];
dc.ColumnMapping = MappingType.Attribute;

将其添加到您的代码中将输出以下内容:

<Customers>
    <CustomerId>272408857</CustomerId>
    <snip>
    <Organizations OrganizationName = "Org1"/>
    <Organizations OrganizationName = "Org2"/>
</Customers>

这并不完全是您所需要的,但我认为您无法使用 .net 工具收集表标记中联接的子节点。您可能必须自己转换 xml。

您还可以将 DataTable 重命名为“Organization”,这样您将拥有一个 Organization 元素列表(尽管仍然缺少父 Organizations 元素)。

你怎么认为?

DataColumn dc = orgTable.Columns["OrganizationName"];
dc.ColumnMapping = MappingType.Attribute;

Adding this to your code will output the following:

<Customers>
    <CustomerId>272408857</CustomerId>
    <snip>
    <Organizations OrganizationName = "Org1"/>
    <Organizations OrganizationName = "Org2"/>
</Customers>

It is not exactly what you need, but I think you cannot collect the child nodes of a join in a table-tag with .net tools. You will probably have to transform the xml by yourself.

You could also rename your DataTable to "Organization" such that you will have a list of Organization Elements (still lacking a parent Organizations element though).

What do you think?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文