zoukankan      html  css  js  c++  java
  • 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;
        }
    }

  • 相关阅读:
    第十四周 Leetcode 315. Count of Smaller Numbers After Self(HARD) 主席树
    POJ1050 To the Max 最大子矩阵
    POJ1259 The Picnic 最大空凸包问题 DP
    POJ 3734 Blocks 矩阵递推
    POJ2686 Traveling by Stagecoach 状态压缩DP
    iOS上架ipa上传问题那些事
    深入浅出iOS事件机制
    iOS如何跳到系统设置里的各种设置界面
    坑爹的私有API
    业务层网络请求封装
  • 原文地址:https://www.cnblogs.com/handboy/p/7141608.html
Copyright © 2011-2022 走看看