Parse XML tree with no id using LINQ to XML
Posted
by
Danny
on Stack Overflow
See other posts from Stack Overflow
or by Danny
Published on 2012-03-14T14:44:28Z
Indexed on
2012/03/27
11:29 UTC
Read the original article
Hit count: 575
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...
© Stack Overflow or respective owner