我目前正在嘗試從xml填充從web請求返回給我的treeview。當響應進來,我操縱數據,以便XML在這個佈局:從XML填充treeview
<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>
所有我想要做的是顯示組名,然後您就可以展開並查看子組。目前我已經得到了它「有點」的工作,但它的全部都在一個線性視圖中。這裏是我的代碼,我目前有:
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();
}
此圖片上面的代碼看起來什麼時,當地我的系統上瀏覽,如:
任何建議,即時通訊相當失落和它做我的進來!
這是否將代替我的addTreeNode方法或取代所有上述代碼? – Sad 2011-12-14 17:05:01
這將代替您的代碼。 – 2011-12-14 17:27:22