始创于2000年 股票代码:831685
咨询热线:0371-60135900 注册有礼 登录
  • 挂牌上市企业
  • 60秒人工响应
  • 99.99%连通率
  • 7*24h人工
  • 故障100倍补偿
您的位置: 网站首页 > 帮助中心>文章内容

Linq to xml操作XML

发布时间:  2012/8/20 17:41:27

.Net中的System.Xml.Linq命名空间提供了linq to xml的支持。这个命名空间中的XDocument,XElement以及XText,XAttribute提供了读写xml文档的关键方法。

1. 使用linq to xml写xml:

使用XDocument的构造函数可以构造一个Xml文档对象;使用XElement对象可以构造一个xml节点元素,使用XAttribute构造函数可以构造元素的属性;使用XText构造函数可以构造节点内的文本。

如下实例代码:

  1. class Program  
  2. {  
  3.     static void Main(string[] args)  
  4.     {             
  5.         var xDoc = new XDocument(new XElement( "root",  
  6.             new XElement("dog",  
  7.                 new XText("dog said black is a beautify color"),  
  8.                 new XAttribute("color", "black")),  
  9.             new XElement("cat"),  
  10.             new XElement("pig", "pig is great")));  
  11.  
  12.         //xDoc输出xml的encoding是系统默认编码,对于简体中文操作系统是gb2312  
  13.         //默认是缩进格式化的xml,而无须格式化设置  
  14.         xDoc.Save(Console.Out);  
  15.  
  16.         Console.Read();  
  17.     }  

上面代码将输出如下Xml:

  1. <?xml version="1.0" encoding="gb2312"?> 
  2. <root> 
  3.   <dog color="black">dog said black is a beautify color</dog> 
  4.   <cat /> 
  5.   <pig>pig is great</pig> 
  6. </root> 

 

可以看出linq to xml比XmlDocument和XmlWriter要方便很多。

2. 使用linq to xml 读取xml

Linq是从集合中查询对象,在linq to xml中的集合是通过XElement的Elements(),Elements(string name),以及Descendants、DescendantsAndSelf、Ancestors、AncestorsAndSelf的几个重载方法中获得。

获得XElement集合之后,可以通过XElement的Attribute(string name)方法获得元素的属性值,可以通过XElement的Value属性获得节点的文本值;使用linq就可以方便的做查询,做筛选排序了

还是上例中的xml,我们要读取root的所有字节点,并打印出来,如下代码:

  1. class Program  
  2. {  
  3.     static void Main(string[] args)  
  4.     {  
  5.              
  6.         var xDoc = new XDocument(new XElement( "root",  
  7.             new XElement("dog",  
  8.                 new XText("dog said black is a beautify color"),  
  9.                 new XAttribute("color", "black")),  
  10.             new XElement("cat"),  
  11.             new XElement("pig", "pig is great")));  
  12.  
  13.         //xDoc输出xml的encoding是系统默认编码,对于简体中文操作系统是gb2312  
  14.         //默认是缩进格式化的xml,而无须格式化设置  
  15.         xDoc.Save(Console.Out);  
  16.  
  17.         Console.WriteLine();  
  18.  
  19.         var query = from item in xDoc.Element( "root").Elements()  
  20.                     select new  
  21.                     {  
  22.                         TypeName    = item.Name,  
  23.                         Saying      = item.Value,  
  24.                         Color       = item.Attribute("color") == null?(string)null:item.Attribute("color").Value  
  25.                     };  
  26.  
  27.  
  28.         foreach (var item in query)  
  29.         {  
  30.             Console.WriteLine("{0} 's color is {1},{0} said {2}",item.TypeName,item.Color??"Unknown",item.Saying??"nothing");  
  31.         }  
  32.  
  33.         Console.Read();  
  34.     }  

3. Linq to xml简单的应用

应用需求: 读取博客园的rss,然后在页面上输出最新的10篇博客信息

实现要点: 通过XDocument的Load静态方法载入Xml,


本文出自:亿恩科技【www.enkj.com】

 

可以看出linq to xml比XmlDocument和XmlWriter要方便很多。

2. 使用linq to xml 读取xml

Linq是从集合中查询对象,在linq to xml中的集合是通过XElement的Elements(),Elements(string name),以及Descendants、DescendantsAndSelf、Ancestors、AncestorsAndSelf的几个重载方法中获得。

获得XElement集合之后,可以通过XElement的Attribute(string name)方法获得元素的属性值,可以通过XElement的Value属性获得节点的文本值;使用linq就可以方便的做查询,做筛选排序了

还是上例中的xml,我们要读取root的所有字节点,并打印出来,如下代码:

  1. class Program  
  2. {  
  3.     static void Main(string[] args)  
  4.     {  
  5.              
  6.         var xDoc = new XDocument(new XElement( "root",  
  7.             new XElement("dog",  
  8.                 new XText("dog said black is a beautify color"),  
  9.                 new XAttribute("color", "black")),  
  10.             new XElement("cat"),  
  11.             new XElement("pig", "pig is great")));  
  12.  
  13.         //xDoc输出xml的encoding是系统默认编码,对于简体中文操作系统是gb2312  
  14.         //默认是缩进格式化的xml,而无须格式化设置  
  15.         xDoc.Save(Console.Out);  
  16.  
  17.         Console.WriteLine();  
  18.  
  19.         var query = from item in xDoc.Element( "root").Elements()  
  20.                     select new  
  21.                     {  
  22.                         TypeName    = item.Name,  
  23.                         Saying      = item.Value,  
  24.                         Color       = item.Attribute("color") == null?(string)null:item.Attribute("color").Value  
  25.                     };  
  26.  
  27.  
  28.         foreach (var item in query)  
  29.         {  
  30.             Console.WriteLine("{0} 's color is {1},{0} said {2}",item.TypeName,item.Color??"Unknown",item.Saying??"nothing");  
  31.         }  
  32.  
  33.         Console.Read();  
  34.     }  

3. Linq to xml简单的应用

应用需求: 读取博客园的rss,然后在页面上输出最新的10篇博客信息

实现要点: 通过XDocument的Load静态方法载入Xml,


本文出自:亿恩科技【www.enidc.com】
-->

服务器租用/服务器托管中国五强!虚拟主机域名注册顶级提供商!15年品质保障!--亿恩科技[ENKJ.COM]

  • 您可能在找
  • 亿恩北京公司:
  • 经营性ICP/ISP证:京B2-20150015
  • 亿恩郑州公司:
  • 经营性ICP/ISP/IDC证:豫B1.B2-20060070
  • 亿恩南昌公司:
  • 经营性ICP/ISP证:赣B2-20080012
  • 服务器/云主机 24小时售后服务电话:0371-60135900
  • 虚拟主机/智能建站 24小时售后服务电话:0371-60135900
  • 专注服务器托管17年
    扫扫关注-微信公众号
    0371-60135900
    Copyright© 1999-2019 ENKJ All Rights Reserved 亿恩科技 版权所有  地址:郑州市高新区翠竹街1号总部企业基地亿恩大厦  法律顾问:河南亚太人律师事务所郝建锋、杜慧月律师   京公网安备41019702002023号
      0
     
     
     
     

    0371-60135900
    7*24小时客服服务热线