-4

I get machine generated XML from several process. They are structured like so:

<Data>
  <Report>
    <Machine name="hostA">
      <MachineInfo location="LA">
        <function name="run">Late</function>
        <function name="status">Complete</function>
        <function name="date">2015-06-14</function>
      </MachineInfo>
      <RepItem name="1488" direction="NS">
        <Desc>None Found</Desc>
        <Status Int="A12">Uplink</Status>
      </RepItem>
      <RepItem name="1489" direction="S">
        <Desc>31Ghz Ant at 285ft.</Desc>
        <Status Int="D5">Active</Status>
      </RepItem>
      <RepItem name="1438" direction="W">
        <Desc>West N. Oc. Backup</Desc>
        <Status Int="A11">Disabled</Status>
      </RepItem>
      <RepItem name="1141" direction="SE">
        <Desc>MDT Co.</Desc>
        <Status Int="B7">Active</Status>
      </RepItem>
    </Machine>
    <Machine name="hostB">
      <MachineInfo location="E. LA">
        <function name="run">Late</function>
        <function name="status">Complete</function>
        <function name="date">2015-06-14</function>
      </MachineInfo>
      <RepItem name="1488" direction="NS">
        <Desc>None Found</Desc>
        <Status Int="A12">Down</Status>
      </RepItem>
      <RepItem name="1489" direction="S">
        <Desc>31Ghz Ant at 285ft.</Desc>
        <Status Int="D5">Active</Status>
      </RepItem>
      <RepItem name="1438" direction="W">
        <Desc>West N. Oc. Backup</Desc>
        <Status Int="A11">Disabled</Status>
      </RepItem>
      <RepItem name="1141" direction="SE">
        <Desc>MDT Co.</Desc>
        <Status Int="B7">Active</Status>
      </RepItem>
    </Machine>
  </Report>
</data>

So I've played around with XDocument and LINQ but I can't figure out how to handle the repeating elements. The last chunk of code I played with looks a bit like this:

XDocument report = XDocument.Load(filename);
foreach (XElement element in report
    .Element("Machine")
    .Elements("RepItem"))
    { 

All I want to do is suck in the XML and parse it so I can load it into a database for record keeping and reporting via a stored procedure.

1 Answers1

0

In this example the XML is sent to the xmlContent string variable and the root of the data(the one repeating) is Table. The cust_id and cust_name are XML tags to read in the class Customer using the same names for the properties.

string xmlContent ="the xml content";           

XDocument doc = XDocument.Parse(xml);

                var data = from item in doc.Descendants("Table")
                           select new Customers
                           {
                               cust_id = XMLManager.TryGetElementValue(item, "cust_id"),
                               cust_name = XMLManager.TryGetElementValue(item, "cust_name"),

                           };
Juan
  • 1,352
  • 13
  • 20