博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LINQ TO XML
阅读量:6535 次
发布时间:2019-06-24

本文共 3970 字,大约阅读时间需要 13 分钟。

XElement config = XElement.Parse ( @"
30
90
"); foreach (XElement child in config.Elements()) child.Name.Dump ("Child element name"); XElement client = config.Element ("client"); bool enabled = (bool) client.Attribute ("enabled"); // Read attribute --client enabled.Dump ("enabled attribute"); client.Attribute ("enabled").SetValue (!enabled); // Update attribute --True int timeout = (int) client.Element ("timeout"); // Read element--30 timeout.Dump ("timeout element"); client.Element ("timeout").SetValue (timeout * 2); // Update element client.Add (new XElement ("retries", 3)); // Add new elememt config.Dump ("Updated DOM"); config.Descendants ("client").Count().Dump ("Count of all clients");//2 config.Elements ("client").Count().Dump("Count of Elements");//2 config.FirstNode.Dump ("FirstNode"); config.FirstNode.AddAnnotation ("Hello FirstNode");//增加注释 config.FirstNode.Annotation
().Dump ("String annotations"); config.FirstNode.RemoveAnnotations
();//移除注释 config.FirstNode.Annotation
().Dump ("String annotations"); config.FirstNode.Dump ("Hello FirstNode"); config.LastNode.Dump ("LastNode"); config.FirstNode.AddAfterSelf(new XElement ("client",new XElement ("timeout",120),new XElement ("retries",30))); config.Dump("After calling items.FirstNode.AddAfterSelf"); config.FirstNode.ReplaceWith (new XComment ("client was here")); config.Dump("Notice to change of FirstNode "); config.FirstNode.Remove(); config.Dump("After"); //config.Add(new XElement ("client",new XElement ("timeout", new XText ("12"),new XText ("0")))); config.Add(new XElement ("client",new XAttribute ("id", "IDProperty"),new XElement ("timeout", "12","0"))); config.Dump("After of Add");

StreamingElement

new XStreamingElement ("customers", from c in Customers where c.ID==4 select new XStreamingElement ("customer", new XAttribute ("id", c.ID), new XElement ("name", c.Name), new XElement ("buys", c.Purchases.Count)             )         )

        Result:    

Mary
3

Transforming an X-DOM

 

void Main() {
XElement project = XElement.Parse (@"
AnyCPU
9.0.11209
"); XNamespace ns = project.Name.Namespace; IEnumerable
paths = from compileItem in project.Elements (ns + "ItemGroup").Elements (ns + "Compile") let include = compileItem.Attribute ("Include") where include != null select include.Value; var query = new XElement ("Project", ExpandPaths (paths)); query.Dump(); } static IEnumerable
ExpandPaths (IEnumerable
paths) {
var brokenUp = from path in paths let split = path.Split (new char[] { '\\' }, 2) orderby split[0] select new {
name = split[0], remainder = split.ElementAtOrDefault (1) }; IEnumerable
files = from b in brokenUp where b.remainder == null select new XElement ("file", b.name); IEnumerable
folders = from b in brokenUp where b.remainder != null group b.remainder by b.name into grp select new XElement ("folder", new XAttribute ("name", grp.Key), ExpandPaths (grp) ); return files.Concat (folders); }

 

result:

 

ObjectGraph.cs
Program.cs
AssemblyInfo.cs
Aggregation.cs
RecursiveXml.cs

 

转载于:https://www.cnblogs.com/sanic/archive/2012/03/27/2420405.html

你可能感兴趣的文章
基于ThinkPHP写的一个简单的CMS系统
查看>>
Exchange 2010 DAG local and Site DR/Failover and Fail back
查看>>
LigerUI - 树表格的数据来自Server
查看>>
认证技术概述
查看>>
2016国赛小结
查看>>
Android Studio 第六十四期 - Android业务组件化之URL Scheme使用
查看>>
Hyper-V 2016 系列教程41 Windows 10 Hyper-V 系统要求
查看>>
EC2 WordPress 移动目录
查看>>
Windows Server 2008 启用公共文件夹共享
查看>>
【运维故事】职场如何领先一步?
查看>>
如何提高SEO优化团队效率
查看>>
SFB 项目经验-17-Windows 2012 R2-补丁打到最新-问题-KB2982006
查看>>
用hadoop中的libhdfs和fuse-dfs构建快速云存储
查看>>
Apple Watch的非“智能手表”卖点
查看>>
fedora17升级到fedora18
查看>>
单例模式(Singleton)
查看>>
函数指针和指针函数
查看>>
Python的函数参数传递:传值?引用?
查看>>
[转]分享2011年8个最新的jQuery Mobile在线教程
查看>>
android call require api level
查看>>