简体   繁体   中英

Parse XML tree with no id using LINQ to XML

Requirement I want to read a XML tree, fill my objects with encountered attributes and after every run a method (insert it into my db). The amount of parents is not specified, also the order is not specified, it could be, address-death-death-address-address for example

Input file

Overview:

 <Root>
  <Element>
    <Element2>
      <Parent>
        <Child>
          <Grandchild>
          <Grandchild>
        </Child>
      </Parent>
    </Element2>
  </Element1>
</Root>

Full example:

<?xml version="1.0" encoding="utf-8" ?>
<Root>
  <Element1>
    <Element2>
      <Parent>
        <Child>
          <Grandchild>
            <number>01</number>
            <name>Person</name>
            <Rows>
              <Row>
                <number>0110</number>
                <name>ID</name>
                <value>123456789</value>
              </Row>
            </Rows>
          </Grandchild>
          <Grandchild>
            <number>08</number>
            <name>Address</name>
            <Rows>
              <Row>
                <number>1110</number>
                <name>street</name>
                <value>first aveneu</value>
              </Row>
              <Row>
                <number>1120</number>
                <name>streetnumber</name>
                <value>345</value>
              </Row>
              <Row>
                <number>1130</number>
                <name>zip</name>
                <value>2938PS</value>
              </Row>
              <Row>
                <number>1160</number>
                <name>country</name>
                <value>Germany</value>
              </Row>
            </Rows>
          </Grandchild>
        </Child>
      </Parent>  
      <Parent>  
        <Child>
          <Grandchild>
            <number>01</number>
            <name>Person</name>
            <Rows>
              <Row>
                <number>0110</number>
                <name>ID</name>
                <value>987654321</value>
              </Row>
            </Rows>
          </Grandchild>
          <Grandchild>
            <number>06</number>
            <name>Death</name>
            <Rows>
              <Row>
                <number>0810</number>
                <name>date</name>
                <value>2012-01-03</value>
              </Row>
              <Row>
                <number>0820</number>
                <name>placeOfDeath</name>
                <value>attic</value>
              </Row>
              <Row>
                <number>0830</number>
                <name>funeral</name>
                <value>burrial</value>
              </Row>
            </Rows>
          </Grandchild>
        </Child>
      </Parent>
    </Element2>
  </Element1>
</Root>

Desired result After encounter of parent determine type of grandchild (number 6 is death number 8 is address) Every parent has ALWAYS grandchild number 1 'Person', the second grandchild is either death or address.

reading first parent

Person person = new Person();
person.ID = value;   <--- filled with 123456789

person.street = value;         <--- filled with first aveneu
person.streetnumber = value;   <--- filled with 345 
person.zip = value;            <--- filled with 2938PS 
person.country = value;        <--- filled with germany

person.DoMethod(); // inserts the value in db

Continue reading next parent.

Person person = new Person();
person.ID = value;     <--- filled with 987654321

person.date           = value;    <--- filled with 2012-01-03
person.placeOfDeath   = value;   <--- filled with attic
person.funeral        = value;   <--- filled with burrial

person.DoMethod();  // insert the values in db

Continue reading till no parents found

EDIT: how do I target the name element of the second grandchild for every child? Like address or death

Code/Credit

I got no further then this, with help of Daniel Hilgarth: Linq to XML (C#) parse XML tree with no attributes/id to object The XML tree has changed, and I am really stuck.. in the meantime I try to post new working code...

It could be done in the manner proposed by Daniel in the question your mentioned. You need to create a dictionary from Row nodes (values of name and value nodes) for each Child node, and then iterate those key-value pairs to fill out the Person instances:

var doc = XDocument.Parse(xml);
var children = doc.Descendants("Child")
    .Select(c => c.Descendants("Row")
        .ToDictionary(
            r => r.Element("name").Value,
            r => r.Element("value").Value
        )
    );

foreach (var child in children)
{
    Person person = new Person();
    foreach (var key in child.Keys)
    {
        switch (key)
        {
            case "ID":
                person.ID = int.Parse(child[key]);
                break;
            case "street":
                person.street = child[key];
                break;
            case "streetnumber":
                person.streetnumber = child[key];
                break;
            case "zip":
                person.zip = child[key];
                break;
            case "country":
                person.country = child[key];
                break;
            case "date":
                person.date = DateTime.Parse(child[key]);
                break;
            case "placeOfDeath":
                person.placeOfDeath = child[key];
                break;
            case "funeral":
                person.funeral = child[key];
                break;
            default:
                break;
        }
    }

    person.DoMethod();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM