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");