2010-05-05 94 views
1

使用.net窗體窗體應用程序的Im。我有一個XML文件。我想用xml文件中的數據填充一個樹視圖。我正在使用下面的代碼。用xml文件填充樹視圖

private void button1_Click(object sender, EventArgs e) 
    { 
     try 
     { 
      this.Cursor = System.Windows.Forms.Cursors.WaitCursor; 

      //string strXPath = "languages"; 
      string strRootNode = "Treeview Sample"; 

      OpenFileDialog Dlg = new OpenFileDialog(); 
     Dlg.Filter = "All files(*.*)|*.*|xml file (*.xml)|*.txt"; 
     Dlg.CheckFileExists = true; 
     string xmlfilename = ""; 


     if (Dlg.ShowDialog() == DialogResult.OK) 
     { 
      xmlfilename = Dlg.FileName; 

     } 



      // Load the XML file. 
      //XmlDocument dom = new XmlDocument(); 
      //dom.Load(xmlfilename); 

      XmlDocument doc = new XmlDocument(); 
      doc.Load(xmlfilename); 
      string rootName = doc.SelectSingleNode("/*").Name; 
      textBox4.Text = rootName.ToString(); 
      //XmlNode root = dom.LastChild; 
      //textBox4.Text = root.Name.ToString(); 


      // Load the XML into the TreeView. 
      this.treeView1.Nodes.Clear(); 
      this.treeView1.Nodes.Add(new TreeNode(strRootNode)); 
      TreeNode tNode = new TreeNode(); 
      tNode = this.treeView1.Nodes[0]; 

      XmlNodeList oNodes = doc.SelectNodes(textBox4.Text); 
      XmlNode xNode = oNodes.Item(0).ParentNode; 

      AddNode(ref xNode, ref tNode); 

      this.treeView1.CollapseAll(); 
      this.treeView1.Nodes[0].Expand(); 
      this.Cursor = System.Windows.Forms.Cursors.Default; 
     } 

     catch (Exception ex) 
     { 
      this.Cursor = System.Windows.Forms.Cursors.Default; 
      MessageBox.Show(ex.Message, "Error"); 
     } 
    } 
    private void AddNode(ref XmlNode inXmlNode, ref TreeNode inTreeNode) 
    { 
     // Recursive routine to walk the XML DOM and add its nodes to a TreeView. 
     XmlNode xNode; 
     TreeNode tNode; 
     XmlNodeList nodeList; 
     int i; 

     // Loop through the XML nodes until the leaf is reached. 
     // Add the nodes to the TreeView during the looping process. 
     if (inXmlNode.HasChildNodes) 
     { 
      nodeList = inXmlNode.ChildNodes; 
      for (i = 0; i <= nodeList.Count - 1; i++) 
      { 
       xNode = inXmlNode.ChildNodes[i]; 
       inTreeNode.Nodes.Add(new TreeNode(xNode.Name)); 
       tNode = inTreeNode.Nodes[i]; 
       AddNode(ref xNode, ref tNode); 
      } 
     } 
     else 
     { 
      inTreeNode.Text = inXmlNode.OuterXml.Trim(); 
     } 
    } 

我的XML文件是這樣的:「的hello.xml」

<?xml version="1.0" encoding="utf-8" ?> 
<languages> 
    <language> 
    <key>abc</key> 
    <value>hello how ru</value> 
    </language> 
    <language> 
    <key>def</key> 
    <value>i m fine</value> 
    </language> 
    <language> 
    <key>ghi</key> 
    <value>how abt u</value> 
    </language> 
</languages> 

現在使用上面的代碼後,我能夠填充樹視圖。但我不喜歡填充完整的XML文件。我應該得到的只是直到

languages 
    language 
     key 
     value 

我不想

abc 
how are you 
etc..... 

我的意思是說葉節點做。請幫助我

回答

1

如果我理解正確,你不想添加的是節點的文本內容;那麼你可以檢查XmlNode類的NodeType屬性,如:

xNode = inXmlNode.ChildNodes[i]; 
if (xNode.NodeType != XmlNodeType.Text) { 
    inTreeNode.Nodes.Add(new TreeNode(xNode.Name)); 
    tNode = inTreeNode.Nodes[i]; 
    AddNode(ref xNode, ref tNode); 
}