2016-02-12 68 views
1

IP地址未顯示在樹形視圖中。IP地址未顯示在樹形視圖中

這裏是我的XML代碼:

<?xml version="1.0" standalone="yes"?> 
<config> 
    <service name="router"> 
    <ip_address>10.0.3.1</ip_address> 
    <action>active</action> 
    </service> 
    <service name="sname2"> 
    <ip_address>10.0.3.2</ip_address> 
    <weight>4</weight> 
    <action>active</action> 
    </service> 
    <service name="sname3"> 
    <ip_address>10.0.3.3</ip_address> 
    <weight>5</weight> 
    <protocol>udp</protocol> 
    <action>suspend</action> 
    </service> 
    <service name="nick"> 
    <ip_address>10.0.3.93</ip_address> 
    <action>active</action> 
    </service> 
    <owner name="test"> 
    <content name="rule"> 
     <vip_address>10.0.3.100</vip_address> 
     <protocol>udp</protocol> 
     <port>8080</port> 
     <add_service>nick</add_service> 
     <action>active</action> 
    </content> 
    </owner> 
</config> 

在樹視圖中,IP地址不顯示,我從XML文件加載所以這裏是我的代碼和屏幕,任何人都可以知道這個問題?

enter image description here

using System.Xml; 
using System.IO; 

namespace howto_load_treeview_from_xml 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      string filename = Application.StartupPath; 
      filename = System.IO.Path.Combine(filename, "..\\.."); 
      filename = Path.GetFullPath(filename) + "\\test.xml"; 
      LoadTreeViewFromXmlFile(filename, trvItems); 
     } 

     // Load a TreeView control from an XML file. 
     private void LoadTreeViewFromXmlFile(string filename, TreeView trv) 
     { 
      // Load the XML document. 
      XmlDocument xml_doc = new XmlDocument(); 
      xml_doc.Load(filename); 

      // Add the root node's children to the TreeView. 
      trv.Nodes.Clear(); 
      AddTreeViewChildNodes(trv.Nodes, xml_doc.DocumentElement); 
     } 

     // Add the children of this XML node 
     // to this child nodes collection. 
     private void AddTreeViewChildNodes(TreeNodeCollection parent_nodes, XmlNode xml_node) 
     { 
      foreach (XmlNode child_node in xml_node.ChildNodes) 
      { 
       // Make the new TreeView node. 
       TreeNode new_node = parent_nodes.Add(child_node.Name); 

       // Recursively make this node's descendants. 
       AddTreeViewChildNodes(new_node.Nodes, child_node); 

       // If this is a leaf node, make sure it's visible. 
       if (new_node.Nodes.Count == 0) new_node.EnsureVisible(); 
      } 
     } 
     } 
    } 
+0

@Mairaj艾哈邁德擴大了加載代碼! –

回答

2

#text那些一堆得到,因爲你使用的TreeView節點名稱文本節點Name屬性顯示在樹視圖。爲了讓顯示的文本節點的實際值,使用Value屬性來代替,像這樣(未經測試的代碼):

private void AddTreeViewChildNodes(TreeNodeCollection parent_nodes, XmlNode xml_node) 
{ 
    foreach (XmlNode child_node in xml_node.ChildNodes) 
    { 
     var isTextNode = child_node.NodeType == XmlNodeType.Text; 
     var treeNodeName = isTextNode ? child_node.Value : child_node.Name; 

     // Make the new TreeView node. 
     TreeNode new_node = parent_nodes.Add(treeNodeName); 

     // Recursively make this node's descendants. 
     AddTreeViewChildNodes(new_node.Nodes, child_node); 

     // If this is a leaf node, make sure it's visible. 
     if(new_node.Nodes.Count == 0) new_node.EnsureVisible(); 
    } 
} 
+0

var isTextNode = child_node == XmlNodeType.Text;顯示錯誤 –

+0

我的不好,已更新。應該已經'child_node.NodeType == XmlNodeType.Text'我認爲 – har07

+1

謝謝分配兄弟...問題解決了! –