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.

Was it helpful?

Solution

Here is an Idea:

  1. Parse the XML File and iterate for each ticket the subject and get the value for each <field-id>12345</field-id>

  2. fill all data in a Dictionary with subject as Key and the value as a Value, if not found leave it empty

OTHER TIPS

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();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top