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

mrnian2008 2014-01-25 12:06:05
本人初学linq to xml,深感这是一件利器,奈何本人天资愚钝,理解能力一般,想速成有点困难。为了较快的学会linq to xml生成树并查询的问题,现在想写个小程序,遍历本地目录,将文件/目录连带其属性,生成XML树存入XML文件,再从界面中加载xml文件,并搜索特定文件。烦请高手赐教,多谢!
...全文
7488 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
exception92 2014-02-26
  • 打赏
  • 举报
回复
简单点;

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(路径)
朝三暮四 2014-02-26
  • 打赏
  • 举报
回复
学习一下这个方法吧 http://www.cnblogs.com/yukaizhao/archive/2011/07/21/linq-to-xml.html
q107770540 2014-01-25
  • 打赏
  • 举报
回复

8,497

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 LINQ
社区管理员
  • LINQ
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧