LINQ to XML

该篇文章是我于2009年6月10日通过自己编写的工具,批量从位于在博客园的博客站点(http://chenxizhang.cnblogs.com)同步而来。文章中的图片地址仍然是链接到博客园的。特此说明!

陈希章

原文地址:http://www.cnblogs.com/chenxizhang/archive/2008/10/27/1320505.html
原文标题:LINQ to XML
原文发表:2008/10/27 7:39:00

这套机制将加快读写XML文档的效率,以及可以直接利用LINQ进行查询。

现在有一个专门的System.Xml.Linq的命名空间

            XDocument srcTree = new XDocument(
                new XComment("This is a comment"),
                new XElement("Root",
                    new XElement("Child1", "data1"),
                    new XElement("Child2", "data2"),
                    new XElement("Child3", "data3"),
                    new XElement("Child2", "data4"),
                    new XElement("Info5", "info5"),
                    new XElement("Info6", "info6"),
                    new XElement("Info7", "info7"),
                    new XElement("Info8", "info8"),
                    new XElement("Test","Chenxizhang",new XAttribute("ID",10248))
                )
            );
 
            Console.WriteLine(srcTree);
 
            XDocument doc = new XDocument(
                new XComment("This is a comment"),
                new XElement("Root",
                    from el in srcTree.Element("Root").Elements()
                    where ((string)el).StartsWith("data")
                    select el
                )
            );
            Console.WriteLine(doc);
            Console.Read();

使用命名空间的例子

http://msdn.microsoft.com/zh-tw/library/bb387075.aspx

 


            XNamespace aw = "http://www.adventure-works.com";

           XElement root = new XElement(aw + "Root",new XAttribute(XNamespace.Xmlns + "aw", "http://www.adventure-works.com"),new XElement(aw + "Child", "child content"));
            Console.WriteLine(root);

            Console.Read();
作者:陈希章
出处:http://blog.youkuaiyun.com/chen_xizhang
本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值