I am currently trying to populate a treeview from xml that is returned to me from a web request. When the response comes in I am manipulating the data so that the XML is in this layout:
<GroupList>
<Group>
<GroupName>my first test group</GroupName>
<GroupID>djnsldgnljsdngljsdngljns</GroupID>
<AccessLevel>high</AccessLevel>
<SubGroup>
<SubGroupName>my first test subgroup</SubGroupName>
<SubGroupID>djnsldgnljsdngljsdngljns</SubGroupID>
</SubGroup>
</Group>
<Group>
<GroupName>my second test group</GroupName>
<GroupID>djnsldgnljsdngljsdngl</GroupID>
<AccessLevel>high</AccessLevel>
<SubGroup>
<SubGroupName>my second test subgroup</SubGroupName>
<SubGroupID>DBXRdjnsldgnljsdngljsdngl</SubGroupID>
</SubGroup>
<SubGroup>
<SubGroupName>my second test subgroup1</SubGroupName>
<SubGroupID>EJdjnsldgnljsdngljsdngl42</SubGroupID>
</SubGroup>
</Group>
</GroupList>
All I want to do is display the groupName and then you are able to expand and view the subgroups. Currently I have got it “sort of” working but its all in one linear view. Here is my code I currently have:
xmlDoc.LoadXml(response2);
groupsTreeView.Nodes.Clear();
groupsTreeView.Nodes.Add(new
TreeNode(xmlDoc.DocumentElement.InnerText));
TreeNode tNode = new TreeNode();
tNode = (TreeNode)groupsTreeView.Nodes[0];
addTreeNode(xmlDoc.DocumentElement, tNode);
groupsTreeView.ExpandAll();
//This function is called recursively until all nodes are loaded
private void addTreeNode(XmlNode xmlNode, TreeNode treeNode)
{
XmlNode xNode;
TreeNode tNode;
XmlNodeList xNodeList;
if (xmlNode.HasChildNodes) //The current node has children
{
xNodeList = xmlNode.ChildNodes;
for (int x = 0; x <= xNodeList.Count - 1; x++)
//Loop through the child nodes
{
xNode = xmlNode.ChildNodes[x];
groupsTreeView.Nodes.Add(new TreeNode(xNode.Value));
tNode = groupsTreeView.Nodes[x];
addTreeNode(xNode, tNode);
}
}
else //No children, so add the outer xml (trimming off whitespace)
treeNode.Text = xmlNode.OuterXml.Trim();
}
This image is what the above code looks like when browsed on my system locally:

Any suggestions, im quite lost and it’s doing my head in!
You could try using this using the linq xml(System.Xml.Linq):
You would call it like this
myTreeView.Nodes.Add(TNGroups(groupsXML)).To load you XML into an element just use
XElement.Load.