XLINQ 访问 XML 文件的方法

XLINQ 的例子我们就写稍稍复杂点,通过获得博客的RSS,然后把RSS中的链接和标题打印出来:

下面就是这个功能的演示代码:

using System;
using System.Linq;
using System.Xml.Linq;

public class XLINQ
{
    
public static void DoSomeThing()
    
{
        XElement feed 
= XElement.Load(”http://blog.joycode.com/ghj/Rss.aspx”);
        if (feed.Element(”channel”) == null)
            
return;

        var rss 
= from item in feed.Element(”channel”).Elements(”item”)
                  select 
new
                  
{
                      title 
= item.Element(”title”).Value,
                      link 
= item.Element(”link”).Value
                  }
;
        
foreach (var item in rss)
        
{
            Console.WriteLine(item.link);
            Console.WriteLine(item.title);
            Console.WriteLine(”
*****”);
        }

    }

}


XLINQ 加载数据的核心就在于 XElement.Load
posted @ 2007-07-06 16:15  RicoRui  阅读(639)  评论(0编辑  收藏  举报