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 = ...
随机推荐
- Bootstrap历练实例:带链接的警告
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- Java中什么是匿名对象,空参构造方法输出创建了几个匿名对象,属性声明成static
package com.swift; //使用无参构造方法自动生成对象,序号不断自增 public class Person { private static int count; //如果在定义类时 ...
- 几种排序算法的比较转自http://blog.csdn.net/keenweiwei/article/details/3697452
1冒泡排序: 已知一组无需数据a[1],a[2],a[3],a[4],a[5][a[n],将其按升序排列,首先找出这组数据中最大值,将a[1]与a[2]比较,若a[1]大,则交换两者的值,否则不变,在 ...
- JS - 生成UUID
function uuid(len, radix) { var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvw ...
- logback写日志
https://blog.csdn.net/u010128608/article/details/76618263 https://blog.csdn.net/zhuyucheng123/articl ...
- 13Shell脚本—编写简单脚本
1. 概述 Shell脚本命令的工作方式有两种:交互式和批处理. 交互式(Interrctive): 用户每输入一条命令就立即执行. 批处理(Batch): 由用户事先编写好一个完整的 Shell 脚 ...
- C++输入密码不显示明文
之前有遇到需求说输入密码不显示明文,但同时会有一些其他问题,暂时没做,如今经过尝试可以实现,但是得先知道要输入的是密码.主要利用的getch()函数的不回显特点.需要注意的是这个函数不是标准函数,而且 ...
- python向上取整 向下取整
向上取整 ceil() 函数返回数字的向上取整整数,就是返回大于等于变量的最近的整数. ceil()是不能直接访问的,需要导入 math 模块. import math math.ceil( x ) ...
- P3391 【模板】文艺平衡树FHQ treap
P3391 [模板]文艺平衡树(Splay) 题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转 ...
- JAVA 基础--开发环境 vscode 搭建
对于使用 Visual Studio Code 的 Java 开发者来说,Language Support for Java(TM) by Red Hat 扩展提供了非常好的语言特性支持,比如智能感知 ...