开发者

DataTable.WriteXML and Child Nodes

I've got the following snippet of code:

        var dataSet = new DataSet("Data");
        var table = GetCustomerTable();
        v开发者_运维问答ar 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?


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜