原文发布时间为: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. python-下拉框

    首先,从selenium.webdriver.support.ui里调用Select类,如下: 其次,找到下拉框元素,再找下拉框里要最终选择的元素,如下: 注意:调用Select类后,不必再加clic ...

  2. sessionStorage对象

    sessStorage对象是一个会话形式的数据存储,当用户关闭浏览器的窗口后,数据就会被删除. 实例: <!DOCTYPE html><html><head> &l ...

  3. Nginx: ubuntu系统上如何判断是否安装了Nginx?

    问题描述:ubuntu系统上,如何查看是否安装了Nginx? 解决方法:输入命令行:ps -ef | grep nginx master process后面就是Nginx的安装目录. 延伸:1. 如何 ...

  4. Shell脚本调用ftp上传文件

    Shell脚本调用ftp上传文件 1.脚本如下 ftp -n<<! open x.x.x.x ###x.x.x.x为ftp地址 user username password ###user ...

  5. CocoaPods在OS X Yosemite上突然不能用了的解决办法

    最近开发的时候发现自己的CocoaPods不能使用了! 根据报的错误上网搜寻answer,于是搜到了解决办法 在 OS X Yosemite 报这样的错: [MT] DVTAssertions: AS ...

  6. 【思维题 线段树】cf446C. DZY Loves Fibonacci Numbers

    我这种maintain写法好zz.考试时获得了40pts的RE好成绩 In mathematical terms, the sequence Fn of Fibonacci numbers is de ...

  7. mariadb多源主从复制错误跳过.md

    mysql 的主从错误跳过和mariadb的多源主从复制错误跳过操作不同,请注意: 更改会话的default_master_connection变量 STOP SLAVE 'slave_account ...

  8. Laravel常用命令

    php artisan make:controller BlogController php artisan make:model Blog

  9. strace用法

    strace   --  trace system calls and signals     strace是Linux环境下的一款程序调试工具,用来监察一个应用程序所使用的系统调用及它所接收的系统信 ...

  10. PHP数组函数 array_multisort() ----对多个数组或多维数组进行排序

    PHP中array_multisort可以用来一次对多个数组进行排序,或者根据某一维或多维对多维数组进行排序. 关联(string)键名保持不变,但数字键名会被重新索引. 输入数组被当成一个表的列并以 ...