首页  编辑  

Xml显示到TreeView

Tags: /C#/文件目录操作/Xml/   Date Created:

using System . IO;

using System . Xml;

private void XmlFile2TreeNodes( string AXmlFile , TreeNodeCollection ATreeNodes)

{

    if ( ! File . Exists(AXmlFile)) return ;

    if (ATreeNodes == null ) return ;

   ATreeNodes . Clear();

    XmlDocument vXmlDocument = new XmlDocument ();

   vXmlDocument . Load(AXmlFile);

   XmlNode2TreeNodes(vXmlDocument . DocumentElement, ATreeNodes);

}

private void XmlNode2TreeNodes( XmlNode AXmlNode, TreeNodeCollection ATreeNodes)

{

    if (AXmlNode == null || ATreeNodes == null ) return ;

    TreeNode vTreeNode = ATreeNodes . Add(AXmlNode . Name);

    if (AXmlNode . NodeType == XmlNodeType . Text)

       vTreeNode . Text = "\"" + AXmlNode . Value + "\"" ;

    foreach ( XmlNode vXmlNode in AXmlNode . ChildNodes)

       XmlNode2TreeNodes(vXmlNode, vTreeNode . Nodes);

}

private void button1_Click( object sender , EventArgs e1)

{

   XmlFile2TreeNodes( @"c:\temp\temp.xml" , treeView1 . Nodes);

}