The original Published time: 2008-08-10--from my Baidu article [imported by moving tools]
Directorytoxml class:
Using System;
Using System.Data;
Using System.Configuration;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Web.UI.HtmlControls;
Using System.IO;
Using System.Xml;
<summary>
Summary description of Directorytoxml
</summary>
public class Directorytoxml
{
Public Directorytoxml ()
{
//
TODO: Add constructor logic here
//
}
public static XmlDocument Createxml (String fpath)
{
XmlDocument myxml = new XmlDocument ();
XmlDeclaration decl = myXML. Createxmldeclaration ("1.0", "Utf-8", null);
myXML. AppendChild (decl);
XmlElement root = myXML. CreateElement (Fpath. Substring (Fpath. LastIndexOf ("\ \") + 1);
myXML. AppendChild (root);
DirectoryInfo di = new DirectoryInfo (Fpath);
foreach (FileSystemInfo fsi in Di. Getfilesysteminfos ())
{
If (FSI is FileInfo)
{
FileInfo fi = (FileInfo) FSI;
XmlElement file = myXML. createelement ("file");
File. InnerText = fi. Name;
File. SetAttribute ("path", fi. FullName);
File. SetAttribute ("name", Fi.) Name);
Root. AppendChild (file);
}
Else
{
DirectoryInfo Childdi = (DirectoryInfo) FSI;
XmlElement dir = myxml. CreateElement ("dir");
Dir. InnerText = Childdi. Name;
Dir. SetAttribute ("Path", Childdi. FullName);
Dir. SetAttribute ("name", Childdi. Name);
Root. AppendChild (dir);
CreateNode (Childdi,dir,myxml);
}
}
return myxml;
}
public static void CreateNode (DirectoryInfo childdi, XmlElement xe,xmldocument DOM)
{
foreach (FileSystemInfo FSI in Childdi. Getfilesysteminfos ())
{
If (FSI is FileInfo)
{
FileInfo fi = (FileInfo) FSI;
XmlElement file = dom. createelement ("file");
File. InnerText = fi. Name;
File. SetAttribute ("path", fi. FullName);
File. SetAttribute ("name", Fi.) Name);
Xe. AppendChild (file);
}
Else
{
DirectoryInfo di = (DirectoryInfo) FSI;
XmlElement Childxe = dom. CreateElement ("dir");
Childxe. InnerText = di. Name;
Childxe. SetAttribute ("Path", Di.) FullName);
Childxe. SetAttribute ("name", Di.) Name);
Xe. AppendChild (CHILDXE);
CreateNode (Di,childxe,dom);
}
}
}
}
----------------------------------------------
Using System;
Using System.Data;
Using System.Configuration;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Web.UI.HtmlControls;
Using System.IO;
Using System.Xml;
public partial class _default:system.web.ui.page
{
protected void Page_Load (object sender, EventArgs e)
{
if (! IsPostBack)
{
TextBox1.Text = Server.MapPath (".");
}
}
protected void Button1_Click (object sender, EventArgs e)
{
string fpath = TextBox1.Text;
XmlDocument dom = Directorytoxml.createxml (Fpath);
Dom. Save (Server.MapPath ("~/app_data/dirlist.xml"));
}
protected void button2_click (object sender, EventArgs e)
{
String xmlpath = Server.MapPath ("~/app_data/dirlist.xml");
if (file.exists (Xmlpath))
{
XmlDocument dom = new XmlDocument ();
Dom. Load (Xmlpath);
TreeView1.Nodes.Clear ();
Bindxmltotreeview (DOM. Documentelement.childnodes, Treeview1.nodes);
}
Else
{
Response.Write ("<script>alert (' XML document does not exist, please create first ') </script>");
}
}
protected void Bindxmltotreeview (XmlNodeList xmlnodes, treenodecollection treenodes)
{
foreach (XmlNode child in XMLNodes)
{
if (child. Attributes! = null && child. Attributes.count > 0)//This judgment is very important!
{
String treetext = child. attributes["Name"]. Value;
TreeNode tn = new TreeNode (treetext);
Treenodes.add (TN);
Bindxmltotreeview (child. ChildNodes, TN. ChildNodes);
}
}
}
protected void Treeview1_selectednodechanged (object sender, EventArgs e)
{
Label1.Text = TreeView1.SelectedNode.Text;
}
}
NET9: The disk catalog file is saved to an XML document and its XML document reads and writes, and the binding XML to the TreeView