Nested XML to DataTable
Question
I have the following XML:
<tickets>
<ticket>
<subject>ABC</subject>
<entries-field>
<entry-field>
<field-id>12345</field-id>
<value>New</value>
</entry-field>
<entries-field>
<ticket>
<ticket>
<subject>DEF</subject>
<entries-field>
<entry-field>
<field-id>67890</field-id>
<value>category_cc</value>
</entry-field>
<entry-field>
<field-id>12345</field-id>
<value>Pending</value>
</entry-field>
<entries-field>
<ticket>
<ticket>
<subject>GHI</subject>
<ticket>
</tickets>
I need to convert it into C# data table as following:
Subject Status
------------------------
ABC New
DEF Pending
GHI
I try many methods but no luck. Kindly appreciate if anyone can give a good idea. Thank you very much.
La solution
Here is an Idea:
Parse the XML File and iterate for each
ticket
thesubject
and get thevalue
for each<field-id>12345</field-id>
fill all data in a Dictionary with
subject
as Key and thevalue
as a Value, if not found leave it empty
Autres conseils
Assuming the real xml you work with is not corrupt as above
XDocument xDoc = XDocument.Parse(xml);
var result = xDoc.Descendants("ticket")
.Select(n => new
{
Subject = n.Element("subject").Value,
Status = (string)n.Descendants("value").LastOrDefault()
})
.ToList();
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow