net9:磁盘目录文件保存到XML文档及其XML文档的读写操作,以及绑定XML到treeview
2021-06-29 12:06
标签:tree name config ack dex web net root element 原文发布时间为:2008-08-10 —— 来源于本人的百度文章 [由搬家工具导入] directorytoxml类: using System; public static void createNode(DirectoryInfo childdi, XmlElement xe,XmlDocument dom) ---------------------------------------------- using System; using System.IO; public partial class _Default : System.Web.UI.Page protected void BindXmlToTreeView(XmlNodeList xmlnodes, TreeNodeCollection treeNodes) protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e) net9:磁盘目录文件保存到XML文档及其XML文档的读写操作,以及绑定XML到treeview 标签:tree name config ack dex web net root element 原文地址:http://www.cnblogs.com/handboy/p/7141608.html
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;
///
/// directorytoxml 的摘要说明
///
public class directorytoxml
{
public directorytoxml()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
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;
}
{
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.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.Xml;
{
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("");
}
}
{
foreach (XmlNode child in xmlnodes)
{
if (child.Attributes != null && child.Attributes.Count > 0)//这个判断很重要!
{
string treeText = child.Attributes["name"].Value;
TreeNode tn = new TreeNode(treeText);
treeNodes.Add(tn);
BindXmlToTreeView(child.ChildNodes, tn.ChildNodes);
}
}
}
{
Label1.Text = TreeView1.SelectedNode.Text;
}
}
文章标题:net9:磁盘目录文件保存到XML文档及其XML文档的读写操作,以及绑定XML到treeview
文章链接:http://soscw.com/index.php/essay/99367.html