热门:网页模板.net视频教程JQueryMVCjsonExtJs源码示例三级联动JQuery菜单
您现在的位置:.Net中文社区>> .Net编程>>正文内容

使用Linq Xml 来读取一个目录下所有的文件并保存为Xml文档

发布时间:2009年06月10日点击数: 未知

 

public static XElement ToXml(this DirectoryInfo Dir)
        {
            XElement Root = new XElement("Dir", new XAttribute("Name", Dir.Name),new XAttribute("文件数",Dir.GetFiles().Count()), from SubDir in Dir.GetDirectories() select ToXml(SubDir), from F in Dir.GetFiles() select new XElement("文件", new List<XElement>{new XElement("文件名", F.Name),new XElement("扩展名",F.Extension)}));
            return Root;
        }

应用的代码

XDocument Doc=new XDocument();
             DirectoryInfo Dir=new DirectoryInfo("E://");
            Doc.Add(LinqEach.ToXml(Dir));
             string Url = Server.MapPath("/");
            if (!Directory.Exists(Url)) { Directory.CreateDirectory(Url); }
            Doc.Save(Url + "AA.xml");

本站热点业务

更多模板/案例展示

关于我们 | 联系我们 | 团队日志 | 网站地图 | 网站合作