I’m using DataRelation to nest tables in a DataSet, to get a XML result like this:
<Customer>
<Name>John</Name>
<Age>30</Age>
<Sex>Male</Sex>
<Order>
<Id>123</Id>
<Price>200.00</Price>
</Order>
</Customer>
I want to group the data by a field value, like sex, to get a result like this:
<Male>
<Customer>
<Name>John</Name>
<Age>30</Age>
<Order>
<Id>123</Id>
<Price>200.00</Price>
</Order>
</Customer>
</Male>
How can I achieve this (except by iterating the tables myself and creating new ones)?
Thanks.
If you’re looking for an XML output, you can use Linq-to-XML to get your result. First step is to produce your XML from the DataSet. I imagine it would look something like this:
Load that XML into an XDocument, and then you can use LINQ to perform your groupings and use that result.
The new XDocument now holds the following structure:
Edit Based on the comment of not knowing what’s in the customer element list (other than name and sex), you could change the approach slightly in the query. This will produce the same output as above based on the given XML sample.
Or you can simply select the entire
custelement, which would still include theSextag, which might make more sense to you when you’re processing the records. You still get them into groups, but you do not lose any information at the customer level.