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


    原文发布时间为: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;
    /// <summary>
    /// directorytoxml 的摘要说明
    /// </summary>
    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("<script>alert('XML文档不存在,请先创建')</script>");
            }
        }

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

  • 相关阅读:
    C# Xamarin For Android自动升级项目实战
    C# Xamarin移动开发基础进修篇
    .NET轻量级ORM框架Dapper入门精通
    ASP.NET WebApi技术从入门到实战演练
    (简单、可靠的安装方法)在Windows Server2016中安装SQL Server2016
    ASP.NET (Core) WebAPI IIS PUT和DELETE请求失败 405的解决办法
    js中判断对象是否为空的三种实现方法
    windows10如何设置只显示时间不显示日期
    NuGet微软官方中国国内镜像
    Win10找不到hosts文件解决方法
  • 原文地址:https://www.cnblogs.com/handboy/p/7141608.html
Copyright © 2020-2023  润新知