C# Linq to XML 如何遍历文件目录生成xml树

时间:2021-10-01 12:02:10
本人初学linq to xml,深感这是一件利器,奈何本人天资愚钝,理解能力一般,想速成有点困难。为了较快的学会linq to xml生成树并查询的问题,现在想写个小程序, 遍历本地目录,将文件/目录连带其属性,生成XML树存入XML文件,再从界面中加载xml文件,并搜索特定文件。烦请高手赐教,多谢!

3 个解决方案

#1


 具体化一下你的问题

http://msdn.microsoft.com/zh-CN/vstudio/bb688087

#2


学习一下这个方法吧
http://www.cnblogs.com/yukaizhao/archive/2011/07/21/linq-to-xml.html

#3


简单点;

ComputerEntity entity = tree.CurrentBaseEntity as ComputerEntity;
                        XElement groupElement = new XElement("Group", new XAttribute("Name", entity.GroupName));
                        XElement carsElement = new XElement("Cars", new XAttribute("Name", entity.CollectionName));
                        XElement carElement = new XElement("Car",
                             new XAttribute("Id", entity.Id),
                             new XAttribute("Width", entity.Width),
                             new XAttribute("Height", entity.Height),
                             new XAttribute("IsSelected", entity.IsSelected.ToString()),
                             new XAttribute("CollectionName", entity.CollectionName),
                             new XAttribute("GroupName", entity.GroupName),
                             new XElement("Name", entity.Name),
                             new XElement("IP", entity.IP),
                             new XElement("X", entity.X),
                             new XElement("Y", entity.Y)
                        );
                        carsElement.Add(carElement);
                        groupElement.Add(carsElement);
                        this.xmlDoc.Add(groupElement);

生成xml文件
然后保存:
XElement.Save(路径)

#1


 具体化一下你的问题

http://msdn.microsoft.com/zh-CN/vstudio/bb688087

#2


学习一下这个方法吧
http://www.cnblogs.com/yukaizhao/archive/2011/07/21/linq-to-xml.html

#3


简单点;

ComputerEntity entity = tree.CurrentBaseEntity as ComputerEntity;
                        XElement groupElement = new XElement("Group", new XAttribute("Name", entity.GroupName));
                        XElement carsElement = new XElement("Cars", new XAttribute("Name", entity.CollectionName));
                        XElement carElement = new XElement("Car",
                             new XAttribute("Id", entity.Id),
                             new XAttribute("Width", entity.Width),
                             new XAttribute("Height", entity.Height),
                             new XAttribute("IsSelected", entity.IsSelected.ToString()),
                             new XAttribute("CollectionName", entity.CollectionName),
                             new XAttribute("GroupName", entity.GroupName),
                             new XElement("Name", entity.Name),
                             new XElement("IP", entity.IP),
                             new XElement("X", entity.X),
                             new XElement("Y", entity.Y)
                        );
                        carsElement.Add(carElement);
                        groupElement.Add(carsElement);
                        this.xmlDoc.Add(groupElement);

生成xml文件
然后保存:
XElement.Save(路径)