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

时间: 2024-08-25 13:15:52

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

将XML文件保存到DataGridView中

1 #region get护理单记录信息XML 2 //将XML文件保存到DataTable 3 private DataTable FromXML2DataTable(string XMLStr,string data_h,string data_d) 4 { 5 XmlDocument myDoc = new XmlDocument(); 6 myDoc.LoadXml(XMLStr); 7 if (string.IsNullOrEmpty(XMLStr) || !myDoc.HasChil

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

android如何保存读取读取文件文件保存到SDcard 19. 三 / android基础 / 没有评论 本文来源于www.ifyao.com禁止转载!www.ifyao.com 上图为保存文件的方法体. 上图为如何调用方法体保存数据. 上面的截图介绍了,文件保存的基本内容. 路径也可以更改. 将内容保存到文件介绍完毕. 本文来源于www.ifyao.com禁止转载!www.ifyao.com 读取文件方法体,将方法返回值传给控件即可. 保存文件的四种操作模式 将文件保存到手机的SDcard路

上传 Zip 压缩档并即时解压缩文件保存到 Blob

Upload Zip file and unpack on the fly to Windows Azure Blob Upload Zip file and unpack on the fly to Windows Azure Blob 为求方便(懒惰的做法XD),我直接借用小朱在 边做边学 Windows Azure 应用程序开发基础 Part 2:开发 BLOB.Table 与 Queue 应用程序 一文中的例子来改写. 如图中红色框框:其中最大的不同是使用 DotNetZip 这个组件以

Python 列取两级目录并保存到list.txt中

很多时候需要列出某目录下各个子目录中文件列表并保存到list.txt中,以便于以后遍历该子目录 在图片处理中,常用该方法处理图片目录 目录结构分为3级 rootdir subdir1 pic1 pic2 subdir2 pic1 pic2 想要结果list.txt rootdir 目录下 list.txt 内容为 subdir1 subdir2 subdir1/2目录下 list.txt内容为 pic1 pic2 代码为: 使用方法,把.py放到要提取的当前目录下即可 import os roo

android文件保存到sd卡和内存

1.保存到SD卡中: 获取SDCard的状态: Environment.getExtemalStorageState() Environment.MEDIA_MOUNTED手机装有SDCard,并且可以进行读写 获取SDCard的目录:Environment.getExtemalStorageDirectory() //============================================= File saveFile=new File("/sdcard/zhzhg.txt&q

Linux系统下目录文件配置

刚刚接触Linux,对于Linux系统下的目录配置进行了一些研究,为了避免以后误操作这些目录,建议大家还是记忆一下相关的配置! 总结 Linux 根目录主要配置 目录 文件配置内容 /bin 单用户维护模式下还能被操作的命令 /boot 开机会使用到的文件,包括Linux内核文件以及开机菜单与开机所需配置文件 /dev 设备以及设备接口文件,访问该目录下文件相当于访问某设备 - /dev下的重要文件:/dev/null,/dev/zero,/dev/tty /etc 系统主要的配置文件,比如账号

VB.NET 将JSON格式的字符串保存到XML文件中

1.关于本文 这几天打算写一个工具类JsonXmlHelper,用来进行用XML来保存JSON格式文件的工作.该工具类中要实现2个最主要的函数: 1)将JSON格式的内容写入到地址为address的XML中:WriteJsonToXml 2)把函数1中构造的XML文件恢复成JSON格式文档:RecoverJsonFromXml 函数1的实现将在本文中给出,函数2的实现将在以后发表的博文中给出 2.代码说明 1)添加引用:Newtonsoft.Json.dll 2)导入库 'JSON解析相关函数,

Qt Dom方式写xml,以及保存到xml文件中

#include <QString> #include <QDebug> #include <stdio.h> #include <stdlib.h> #include <QDomDocument> #include <QtXml> //XML DOM的方式 int main(int argc, char *argv[]) {     QByteArray array;     QDomDocument doc;     QDomPr

转:ffmpeg学习(二) 通过rtsp获取H264裸流并保存到mp4文件

本篇将使用上节http://www.cnblogs.com/wenjingu/p/3977015.html中编译好的库文件通过rtsp获取网络上的h464裸流并保存到mp4文件中. 1.VS2010建立VC++  win32控制台项目 2.在工程目录下建立lib目录和include目录,将已编译好的lib拷打lib下,include拷到include下,dll拷到Debug目录下 3.工程属性--配置属性--VC++目录--包含目录,添加ffmpeg头文件目录及其他第三方头文件目录 链接器--常