2011-12-14 207 views
0

我目前正在嘗試從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(); 
    } 

此圖片上面的代碼看起來什麼時,當地我的系統上瀏覽,如:

enter image description here

任何建議,即時通訊相當失落和它做我的進來!

回答

2

你可以嘗試使用這種使用LINQ XML(System.Xml.Linq的):

private TreeNode TNGroups(XElement xml) 
    { 
     TreeNode node = new TreeNode(); 
     foreach (XElement group in xml.Descendants("Group")) 
     { 
      TreeNode tnGroup = new TreeNode(group.Element("GroupName").Value); 
      node.Nodes.Add(tnGroup); 
      foreach (XElement subgroup in group.Elements("SubGroup")) 
      { 
       TreeNode tnSubGroup = new TreeNode(subgroup.Element("SubGroupName").Value); 
       tnGroup.Nodes.Add(tnSubGroup); 
      } 
     } 
     return node; 
    } 

你會這樣稱呼它myTreeView.Nodes.Add(TNGroups(groupsXML))

要將XML加載到元素中,只需使用XElement.Load

+0

這是否將代替我的addTreeNode方法或取代所有上述代碼? – Sad 2011-12-14 17:05:01

+0

這將代替您的代碼。 – 2011-12-14 17:27:22

0

您正確使用遞歸遍歷Xml文件,不幸的是,您在每次迭代時都將Treenodes添加到TreeView的根目錄。相反,修改您的代碼以將子節點添加到循環中正在處理的treenode中,例如,

for (int x = 0; x <= xNodeList.Count - 1; x++) 
    //Loop through the child nodes 
    { 
     xNode = xmlNode.ChildNodes[x]; 
     // Use the treenode, not the treeview!!! 
     treeNode.Nodes.Add(new TreeNode(xNode.Value)); 
     tNode = groupsTreeView.Nodes[x]; 
     addTreeNode(xNode, tNode); 
    } 
+0

嘿,我不能做treeNode.Node,因爲treeNode是類TreeNode而不是Treeview。我已經完成了'treeNode.ChildNodes.Add(new TreeNode(xNode.Value));'這似乎更好。我現在有一個「山姆測試組」的路由元素,它和我的第一組是正確的,它顯示了4個空的複選框,然後是「山姆測試組」作爲子組。它也不會讀取xml中的第二組元素。大壩樹視圖,爲什麼它使它非常複雜! – Sad 2011-12-14 13:26:51

0

我用你的答案代碼,並得到了一些具有屬性和複雜結構的XML未被顯示。混合你所有的想法得到了這個。好像對某人有用

private void populateBaseNodes(XmlDocument docXML) 
    { 
     tView.Nodes.Clear(); // Clear 
     tView.BeginUpdate(); 
     TreeNode treenode; 

     XmlNodeList baseNodeList = docXML.ChildNodes; 

     foreach (XmlNode xmlnode in baseNodeList) 
     { 
      string key = xmlnode.Name == null ? "" : xmlnode.Name.ToString(); 
      string value = xmlnode.Value == null ? xmlnode.Name.ToString() : xmlnode.Value.ToString(); 
      treenode = tView.Nodes.Add(key, value); // add it to the tree 

      if (xmlnode.Attributes.Count > 0) 
      { 
       foreach (XmlAttribute att in xmlnode.Attributes) 
       { 
        TreeNode tnode = new TreeNode(); 
        string _name = att.Name; 
        string _value = att.Value.ToString(); 
        tnode.Name= _name; 
        tnode.ForeColor = Color.Red; 
        tnode.Text= "<Attribute>:" +_name; 
        TreeNode _attvalue = new TreeNode(); 
        _attvalue.Name = _name; 
        _attvalue.Text = _value; 
        _attvalue.ForeColor = Color.Purple; 
        tnode.Nodes.Add(_attvalue); 
        treenode.Nodes.Add(tnode); 
       } 
      } 
      AddChildNodes(xmlnode, treenode); 
     } 
     tView.EndUpdate(); 
     tView.Refresh(); 
    } 

    private void AddChildNodes(XmlNode nodeact, TreeNode TreeNodeAct) 
    { 
     XmlNodeList ChildNodeList = nodeact.ChildNodes; 
     TreeNode aux = null; 

     if (nodeact.HasChildNodes) 
     { 
      //Recursive Call 
      foreach (XmlNode xmlChildnode in nodeact.ChildNodes) 
      { 
       //Add Actual Node & Properties 
       string Key = xmlChildnode.Name == null ? "" : xmlChildnode.Name.ToString(); 
       string Value = xmlChildnode.Value == null ? xmlChildnode.Name.ToString() : xmlChildnode.Value.ToString(); 

       aux = TreeNodeAct.Nodes.Add(Key, Value); 
       AddChildNodes(xmlChildnode, aux); 

       if (xmlChildnode.Attributes != null && xmlChildnode.Attributes.Count > 0) 
       { 
        foreach (XmlAttribute att in xmlChildnode.Attributes) 
        { 
         TreeNode tnode = new TreeNode(); 
         string _name = att.Name; 
         string _value = att.Value.ToString(); 
         tnode.Name = _name; 
         tnode.Text = "<Attribute>:" + _name; 
         tnode.ForeColor = Color.Red; 
         tnode.Text = "<Attribute>:" + _name; 
         TreeNode _attvalue = new TreeNode(); 
         _attvalue.Name = _name; 
         _attvalue.Text = _value; 
         _attvalue.ForeColor = Color.Purple; 
         tnode.Nodes.Add(_attvalue); 
         aux.Nodes.Add(tnode); 
        } 
       } 
      } 
     } 

    } 
1

我需要類似的東西。我喜歡在bferrer的答案的屬性加成,所以我修改了它,並把它放在一個類:

using System; 
using System.Text; 
using System.Xml; 
using System.Drawing; 
using System.Windows.Forms; 

namespace TreeViewTest 
{ 
    class XmlTreeViewBuilder 
    { 
     private XmlDocument xDoc; 
     private TreeView tView; 

     //Constructor with parameters 
     public XmlTreeViewBuilder(XmlDocument xDocument, TreeView treeView) 
     { 
      this.xDoc = xDocument; 
      this.tView = treeView; 
     } 

     public void getTreeView() 
     { 
      tView.Nodes.Clear();          //Clear out the nodes before building 
      XmlNode pNode = xDoc.DocumentElement;      //Set the xml parent node = xml document element 
      string Key = pNode.Name == null ? "" : pNode.Name;   //If null set to empty string, else set to name 
      string Value = pNode.Value == null ? Key : pNode.Value;  //If null set to node name, else set to value 
      TreeNode tNode = tView.Nodes.Add(Key, Value);    //Add the node to the Treeview, set tNode to that node 
      AddTreeNodes(pNode, tNode);         //Call the recursive function to build the tree 
     } 

     //Build out the tree recursively 
     private void AddTreeNodes(XmlNode currentParentNode, TreeNode currentTreeNode) 
     { 
      //Check to see if the node has attributes, if so add them 
      if (currentParentNode.Attributes != null && currentParentNode.Attributes.Count > 0) 
      { 
       foreach (XmlAttribute attrib in currentParentNode.Attributes) 
       { 
        //Create a node for the attribute name 
        TreeNode attribNode = new TreeNode(); 
        attribNode.Name = attrib.Name; 
        attribNode.ForeColor = Color.Red; 
        attribNode.Text = "<Attribute>:" + attrib.Name; 
        //treeNode adds the attribute node 
        currentTreeNode.Nodes.Add(attribNode); 

        //Create a node for the attribute value 
        TreeNode attribValue = new TreeNode(); 
        attribValue.Name = attrib.Name; 
        attribValue.ForeColor = Color.Blue; 
        attribValue.Text = attrib.Value; 
        //Attribute node adds the value node 
        attribNode.Nodes.Add(attribValue); 
       } 
      } 
      //Recursively add children, grandchildren, etc... 
      if (currentParentNode.HasChildNodes) 
      { 
       foreach (XmlNode childNode in currentParentNode.ChildNodes) 
       { 
        string Key = childNode.Name == null ? "" : childNode.Name; 
        string Value = childNode.Value == null ? Key : childNode.Value; 
        TreeNode treeNode = currentTreeNode.Nodes.Add(Key, Value); 
        //Recursive call to repeat the process for all child nodes which may be parents 
        AddTreeNodes(childNode, treeNode); 
       } 
      } 
     } 

    } 
} 

現在從我的形式,我只是有實例化一個對象,並調用getTreeView()像這樣:

 XmlTreeViewBuilder tBuilder = new XmlTreeViewBuilder(xmlDoc, treeView1); 
     tBuilder.getTreeView(); 

我可以根據需要添加更多的功能,如:getNode,setNode等希望這可以幫助某人。