net9:磁盘目录文件保存到XML文档及其XML文档的读写操作,以及绑定XML到treeview

2021-06-29 12:06

阅读:329

标签:tree   name   config   ack   dex   web   net   root   element   

原文发布时间为:2008-08-10 —— 来源于本人的百度文章 [由搬家工具导入]

directorytoxml类:

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;
///


/// 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;
    }

    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("");
        }
    }

    protected void BindXmlToTreeView(XmlNodeList xmlnodes, TreeNodeCollection treeNodes)
    {
        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);
            }
        }
    }

    protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
    {
        Label1.Text = TreeView1.SelectedNode.Text;
    }
}

net9:磁盘目录文件保存到XML文档及其XML文档的读写操作,以及绑定XML到treeview

标签:tree   name   config   ack   dex   web   net   root   element   

原文地址:http://www.cnblogs.com/handboy/p/7141608.html


评论


亲,登录后才可以留言!