linq to xml操作XML

前端之家收集整理的这篇文章主要介绍了linq to xml操作XML前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

http://www.cnblogs.com/yukaizhao/archive/2011/07/21/linq-to-xml.html


LINQ to XML提供了更方便的读写xml方式。前几篇文章评论中总有朋友提,你为啥不用linq to xml?现在到时候了,linq to xml出场了。
.Net中的System.Xml.Linq命名空间提供了linq to xml的支持。这个命名空间中的XDocument,XElement以及XText,XAttribute提供了读写xml文档的关键方法
1. 使用linq to xml写xml:
使用XDocument的构造函数可以构造一个Xml文档对象;使用XElement对象可以构造一个xml节点元素,使用XAttribute构造函数可以构造元素的属性;使用XText构造函数可以构造节点内的文本。
如下实例代码
class Program
{
static void Main( string [] args)
{
var xDoc = new XDocument( new XElement( "root" ,
new XElement( "dog" ,
new XText( "dog said black is a beautify color" ),
new XAttribute( "color" , "black" )),
new XElement( "cat" ),
new XElement( "pig" , "pig is great" )));
//xDoc输出xml的encoding是系统默认编码,对于简体中文操作系统是gb2312
//默认是缩进格式化的xml,而无须格式化设置
xDoc.Save(Console.Out);
Console.Read();
}
}
上面代码输出如下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. var xDoc = new XDocument(new XElement( "root",new XElement("dog",new XText("dog said black is a beautify color"),new XAttribute("color","black")),new XElement("cat"),new XElement("pig","pig is great")));
  6.  
  7. //xDoc输出xml的encoding是系统默认编码,对于简体中文操作系统是gb2312
  8. //默认是缩进格式化的xml,而无须格式化设置
  9. xDoc.Save(Console.Out);
  10.  
  11. Console.WriteLine();
  12.  
  13. var query = from item in xDoc.Element( "root").Elements()
  14. select new
  15. {
  16. TypeName = item.Name,Saying = item.Value,Color = item.Attribute("color") == null?(string)null:item.Attribute("color").Value
  17. };
  18.  
  19.  
  20. foreach (var item in query)
  21. {
  22. Console.WriteLine("{0} 's color is {1},{0} said {2}",item.TypeName,item.Color??"Unknown",item.Saying??"nothing");
  23. }
  24.  
  25. Console.Read();
  26. }
  27. }

3. Linq to xml简单的应用

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

实现要点: 通过XDocument的Load静态方法载入Xml,通过linq查询最新10条数据

代码如下:

  1. <%@ Page Language="C#" AutoEventWireup="true" %>
  2. <script runat="server">
  3. protected override void OnLoad(EventArgs e)
  4. {
  5. //实际应用,通过读取博客园的RSS生成HTML代码显示最新的博客列表
  6. //使用XDocument的Load静态方法载入Xml
  7. //玉开技术博客 http://www.cnblogs.com/yukaizhao
  8. var RSSXDoc = XDocument.Load("http://www.cnblogs.com/RSS");
  9.  
  10. //使用linq to xml查询前10条新博客
  11. var queryBlogs = (from blog in RSSXDoc.Descendants("item")
  12. select new
  13. {
  14. Title = blog.Element("title").Value,Url = blog.Element("link").Value,PostTime = DateTime.Parse(blog.Element("pubDate").Value)
  15. }).Take(20);
  16. repeaterBlogs.DataSource = queryBlogs;
  17. repeaterBlogs.DataBind();
  18. base.OnLoad(e);
  19. }
  20. </script>
  21. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  22. <html xmlns="http://www.w3.org/1999/xhtml">
  23. <head runat="server">
  24. <title>Linq to Xml 实例</title>
  25. </head>
  26. <body>
  27. <ol>
  28. <asp:Repeater ID="repeaterBlogs" EnableViewState="false" runat="server">
  29. <ItemTemplate>
  30. <li><span style="float: right">
  31. <%#Eval("PostTime") %></span><a href="<%#Eval("Url") %>"><%#Eval("Title") %></a></li>
  32. </ItemTemplate>
  33. </asp:Repeater>
  34. </ol>
  35. </body>
  36. </html>
我的微博地址是: http://weibo.com/yukaizhao 我会把一些技术心得碎片写到微博中,欢迎关注。

猜你在找的XML相关文章