原文发布时间为: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基础—抽象类介绍

    一.抽象类介绍

  2. Linux下手动备份还原硬盘主引导记录MBR跟硬盘分区表DPT教程

    Linux下手动备份还原硬盘主引导记录MBR跟硬盘分区表DPT教程 二 18 奶牛 Linux, Ubuntu, Windows 1,885 views查看评论 最近奶牛一直在折腾linux下的gru ...

  3. 一句话懂什么是JS闭包

    无论何时声明新函数并将其赋值给变量,都要存储函数定义和闭包.闭包包含在函数创建时作用域中的所有变量,它类似于背包.函数定义附带一个小背包,它的包中存储了函数定义创建时作用域中的所有变量. 我将永远记住 ...

  4. Find the Longest Word in a String-freecodecamp算法题目

    Find the Longest Word in a String(找出最长单词) 要求 在句子中找出最长的单词,并返回它的长度 函数的返回值应该是一个数字. 思路 用.split(' ')将句子分隔 ...

  5. Android读书笔记四

    第四章 这是一次源代码之旅,学到了如何下载和编译Android源代码和Linux内核源代码.来详细阐述一下一些具体过程 一.Android源代码下载环境 1.安装下载Android源代码的环境配置 ( ...

  6. pandas关联mysql并读写数据库

    1.代码读写mysql,必须安装关联mysql的工具 操作如下命令: sudo apt-get install mysql-server mysql-clientsudo apt-get instal ...

  7. (66)zabbix导入/导出配置文件

    通过导入/导出zabbix配置文件,我们可以将自己写好的模板等配置在网络上分享,我们也可以导入网络上分享的配置文件 配置文件有两种格式,分为为xml与json,通过zabbix管理界面可以导出xml, ...

  8. Unity基础-编辑器

    编辑器 Special Folders Hidden Folder(start with .) Standard Assets:第一批加载的文件 Editor:只在编辑下才能使用, Plugins R ...

  9. EditPlus 比较完整的快捷键记录

    FileFtpUpload Ctrl+Shift+S 上传文件到FTP 服务器 FileNew Ctrl+N 新建普通的文本文档 FileNewHtml Ctrl+Shift+N 创建一个空白的 HT ...

  10. 使用TensorFlow的卷积神经网络识别手写数字(2)-训练篇

    import numpy as np import tensorflow as tf import matplotlib import matplotlib.pyplot as plt import ...