Populate TreeView with XML (C#)





I've been following this answer (C#/XML Populate TreeView with XML file) and trying to adapt it to my XML.

However, I can't make it work right with this XML mainly because of those "orphans":



<group_content>
<parent>
<name>MAIN GROUP</name>
<id>0</id>
</parent>
<group>
<name>THIS AS A PARENT</name>
<id>0001</id>
<group_content>
<parent>
<name>MAIN GROUP</name>
<id>0</id>
</parent>
<srv>
<id>12345</id>
<n>NAME TO SHOW AT CHILD NODES</n>
<uid>RETRIEVE LATER</uid>
</srv>
</group_content>
</group>
(...)
<srv>
<id>4567</id>
<n>ORPHAN CHILD, NO GROUP</n>
<uid>RETRIEVE LATER</uid>
</srv>
</group_content>
</response>


Could someone please shed me some lights about it?

MAIN GROUP can be ignored, I don't need it to show up on TreeView.

THIS IS A PARENT should be the parent node and NAME TO SHOW AT CHILD NODES, as it says, the name to show as a child node.

Problem is with the "orphans", when there's no group they should be the parents.

Also, ignore "RETRIEVE LATER", I'll take care of this when the TreeView is populated.

Thank you.


UPDATE: Would it be best to populate an object instead?

Like described here:

How to parse XML data into the properties of a custom C# class?


No comments:

Post a Comment