Thursday, December 26, 2013

Generating treeview from xml with multiple child nodes per parent.

There are numerous ways that you could structure the XML file, but assuming you can change the structure, I'd be tempted to structure it to be as similar as possible to the desired TreeView layout, perhaps something like



<departments>
<department name="Business Office">
<computers>
<computer>dmorton01</computer>
</computers>
</department>
<department name="Admin">
<computers>
<computer>compu2</computer>
<computer>compu6</computer>
</computers>
</department>
<department name="Advertising">
<computers>
<computer>cosadfsdf</computer>
</computers>
</department>
</departments>



When reading in the XML, you could create a dictionary to store the computers in, for example



Dictionary<string, List<string>> computers = new Dictionary<string, List<string>>();



will allow you to add the department as the key and a list of computers for each department as the associated value. You can then loop through the dictionary and build-up the TreeView nodes.


This isn't the only option and I'm sure others can suggest other ways of handling this, perhaps using LINQ to XML, or some other features.




- HomeGrownCoder My posts are kept as simple as possible for easier understanding. In many cases you can probably optimize or spruce up what I present. Have fun coding!


No comments:

Post a Comment