原文发布时间为: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;
    }
}

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

  1. 将XML文件保存到DataGridView中

    #region get护理单记录信息XML //将XML文件保存到DataTable private DataTable FromXML2DataTable(string XMLStr,string ...

  2. [置顶] Android学习系列-把文件保存到SD卡上面(6)

    Android学习系列-把文件保存到SD卡上面(5) 一般多媒体文件,大文件需要保存到SD卡中.关键点如下: 1,SD卡保存目录:mnt/sdcard,一般采用Environment.getExter ...

  3. 自动将本地文件保存到GitHub

    前言 只有光头才能变强. 文本已收录至我的GitHub精选文章,欢迎Star:https://github.com/ZhongFuCheng3y/3y 这篇文章主要讲讲如何自动将本地文件保存到GitH ...

  4. android如何保存读取读取文件文件保存到SDcard

    android如何保存读取读取文件文件保存到SDcard 本文来源于www.ifyao.com禁止转载!www.ifyao.com 上图为保存文件的方法体. 上图为如何调用方法体保存数据. 上面的截图 ...

  5. Android 将文件保存到SD卡,从卡中取文件,及删除文件

    //保存到SD卡 private static String sdState = Environment.getExternalStorageState();     private static S ...

  6. yii phpexcel自己主动生成文件保存到server上

    近期再整一个报表任务,每天必须把表导出来按excel格式发送邮件给管理员,利用phpexcel把表保存到server上.然后再通过phpmailer发送就ok. ob_end_clean();     ...

  7. Android 将文件保存到SD卡中

    ①写文件到sd卡中需要获得权限,在AndroidManifest.xml中添加如下权限: <uses-permission android:name="android.permissi ...

  8. python 读取一个文件夹下的所jpg文件保存到txt中

    最近需要使用统计一个目录下的所有文件,使用python比较方便,就整理了一下代码. import os def gci(filepath): files = os.listdir(filepath) ...

  9. Android根据URL下载文件保存到SD卡

    //下载具体操作 private void download() { try { URL url = new URL(downloadUrl); //打开连接 URLConnection conn = ...

随机推荐

  1. java中插入myslq的datetime类型的

    java.util.Date d = new java.util.Date(); Timestamp tt=new Timestamp(d.getTime()); ps.setTimestamp(4, ...

  2. 理解AttributeUsage类

    类定义: // 摘要: // 指定另一特性类的用法. 此类不能被继承. [Serializable] [AttributeUsage(AttributeTargets.Class, Inherited ...

  3. 01_5_SERVLET为什么有2个init方法

    01_5_SERVLET为什么有2个init方法 1. 在web.xml配置初始化参数 <servlet> <servlet-name>TestInitServlet</ ...

  4. 【Python项目实战】Pandas:让你像写SQL一样做数据分析(一)

    1. 引言 Pandas是一个开源的Python数据分析库.Pandas把结构化数据分为了三类: Series,1维序列,可视作为没有column名的.只有一个column的DataFrame: Da ...

  5. ccf 201712-2 游戏(Python实现)

    一.原题 问题描述 试题编号: 201712-2 试题名称: 游戏 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 有n个小朋友围成一圈玩游戏,小朋友从1至n编号,2号小朋友坐 ...

  6. 小试nginx日志分析xlog

    nginx配置: http { #...其他配置 log_format tpynormal '$remote_addr | [$time_local] | $host | "$request ...

  7. eclipse使用技巧的网站收集——转载(三)

    本文来自:https://www.cnblogs.com/jeffen/p/5965227.html,未经更改,尊重作者 工欲善其事,必先利其器.对于程序员来说,Eclipse便是其中的一个“器”.本 ...

  8. POJ:3160-Father Christmas flymouse

    Father Christmas flymouse Time Limit: 1000MS Memory Limit: 131072K Description After retirement as c ...

  9. hdu 1011 Starship Troopers(树形背包)

    Starship Troopers Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  10. CodeForces 543D 树形DP Road Improvement

    题意: 有一颗树,每条边是好边或者是坏边,对于一个节点为x,如果任意一个点到x的路径上的坏边不超过1条,那么这样的方案是合法的,求所有合法的方案数. 对于n个所有可能的x,输出n个答案. 分析: 题解 ...