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;
}
}
net9:磁盘目录文件保存到XML文档及其XML文档的读写操作,以及绑定XML到treeview的更多相关文章
- 将XML文件保存到DataGridView中
#region get护理单记录信息XML //将XML文件保存到DataTable private DataTable FromXML2DataTable(string XMLStr,string ...
- [置顶] Android学习系列-把文件保存到SD卡上面(6)
Android学习系列-把文件保存到SD卡上面(5) 一般多媒体文件,大文件需要保存到SD卡中.关键点如下: 1,SD卡保存目录:mnt/sdcard,一般采用Environment.getExter ...
- 自动将本地文件保存到GitHub
前言 只有光头才能变强. 文本已收录至我的GitHub精选文章,欢迎Star:https://github.com/ZhongFuCheng3y/3y 这篇文章主要讲讲如何自动将本地文件保存到GitH ...
- android如何保存读取读取文件文件保存到SDcard
android如何保存读取读取文件文件保存到SDcard 本文来源于www.ifyao.com禁止转载!www.ifyao.com 上图为保存文件的方法体. 上图为如何调用方法体保存数据. 上面的截图 ...
- Android 将文件保存到SD卡,从卡中取文件,及删除文件
//保存到SD卡 private static String sdState = Environment.getExternalStorageState(); private static S ...
- yii phpexcel自己主动生成文件保存到server上
近期再整一个报表任务,每天必须把表导出来按excel格式发送邮件给管理员,利用phpexcel把表保存到server上.然后再通过phpmailer发送就ok. ob_end_clean(); ...
- Android 将文件保存到SD卡中
①写文件到sd卡中需要获得权限,在AndroidManifest.xml中添加如下权限: <uses-permission android:name="android.permissi ...
- python 读取一个文件夹下的所jpg文件保存到txt中
最近需要使用统计一个目录下的所有文件,使用python比较方便,就整理了一下代码. import os def gci(filepath): files = os.listdir(filepath) ...
- Android根据URL下载文件保存到SD卡
//下载具体操作 private void download() { try { URL url = new URL(downloadUrl); //打开连接 URLConnection conn = ...
随机推荐
- 【思维题 线段树】cf446C. DZY Loves Fibonacci Numbers
我这种maintain写法好zz.考试时获得了40pts的RE好成绩 In mathematical terms, the sequence Fn of Fibonacci numbers is de ...
- Docker 守护进程的配置和操作 & 远程访问
守护进程的配置和操作 1.查看守护进程 linux命令: ps -ef | gerp docker sudo status docker 2.开启关闭重启守护进程 sudo service docke ...
- Re:从零开始的Linux之路(目录配置)
基于 Red Hat Enterprise Linux 7.5 或者 CentOS 7.4 FHS协议(Filesystem Hierarchy Standard)——文件系统层次化标准 该标准定义了 ...
- LeetCode之Weekly Contest 102
第一题:905. 按奇偶校验排序数组 问题: 给定一个非负整数数组 A,返回一个由 A 的所有偶数元素组成的数组,后面跟 A 的所有奇数元素. 你可以返回满足此条件的任何数组作为答案. 示例: 输入: ...
- poj-1979 red and black(搜索)
Time limit1000 ms Memory limit30000 kB There is a rectangular room, covered with square tiles. Each ...
- selenium2元素定位Xpath和cssSelector
Selenium2中元素有以下几种定位方法, 常用的有Id,xpath, cssSelector XPATH介绍: XPATH是一种选择器 XPATH在firefox中用firepath验证 XP ...
- 串口编程的相关API函数
用户使用函数CreateFile()创建与指定串口相关联的文件,然后可以使用该函数返回的文件句柄进行串口参数设置.• 01 HANDLE hModem; //定义串口句柄02 hModem=Creat ...
- C++ 虚函数&纯虚函数&抽象类&接口&虚基类(转)
http://www.cnblogs.com/fly1988happy/archive/2012/09/25/2701237.html 1. 多态 在面向对象语言中,接口的多种不同实现方式即为多态.多 ...
- JAVA-基础(五) 更多工具集
1.StringTokenizer(字符串标记) StringTokenizer实现枚举(Enumeration)接口.因此,给定一个输 入字符串,可以使用StringTokenizer对包含于其中的 ...
- JAVA 基础--开发环境Sublime Text 3 搭建
方法一 打开Sublime Text 3,依次点击Preference, Browse Packages,在打开的窗口中双击User文件夹,新建文件JavaC.sublime-build,用记事本打 ...