Whitin a “units” element can be many “unit” elements which again have a “units” element.
I want to write the unitName of each unit into a custom class with prop UnitName AND the unitName of the previous/parent unit into the prop ParentUnitName.
I am also unsure what type of data the recursive method should return… best would be a List with a flat list.
How would you do that?
<units>
<unit>
<unitName>Test05</unitName>
<units>
<unit>
<unitName>Test03</unitName>
<units>
<unit>
<unitName>Test04</unitName>
<units>
<unit>
<unitName>Test07</unitName>
<units>
<unit>
<unitName>Test01</unitName>
</unit>
</units>
</unit>
</units>
</unit>
</units>
</unit>
</units>
</unit>
</units>
Have a look at the XDocument class.
It has a variety of methods available to help with parsing Xml easily. In your case it would look something like:
This will iterate through all
unitNameelements in the xml document and you can do whatever you want with them.Edit: Changed xdoc.Descendants to xdoc.Elements, so it works recursively now and yields the expected results.