使用Linq查询读取RSS提要节点

时间:2014-07-21 15:21:51

标签: c# xml linq rss

在下面的示例中,您可以建议一下阅读的最佳方式" ApplicationUrl"值使用linq。

<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:a10="http://www.w3.org/2005/Atom" version="2.0">
   <channel>
      <title>TestTitle</title>
      <item>
         <a10:content type="application/xml">
            <Vacancy xmlns="http://schemas.datacontract.org/2004/08/Har" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
               <AdvertText i:nil="true" />
               <ApplicationUrl>http://www.Test.co.uk/test/Sec/signin.aspx?vid=685</ApplicationUrl>
            </Vacancy>
         </a10:content>
      </item>
   </channel>
</rss>

我正在尝试使用以下linq查询但仍停留在内容级别

(from node in doc.Elements("channel").Elements("item")
 select new
 {
     Link = "link: " +node.Element("link").Value,
     Title = "Title: "+node.Element("title").Value,
     UpdatedOn ="Updated on : "+  node.Element(atom + "updated").Value,
     //here I have problem for getting Content properly
     Content = "Vacancy : " + XElement(node.Element(atom + "Vacancy"))
 })

1 个答案:

答案 0 :(得分:1)

您可以尝试这种方式:

XNamespace ns = "http://schemas.datacontract.org/2004/08/Har";
XNamespace a10 = "http://www.w3.org/2005/Atom";
......
......
(from node in doc.Elements("channel").Elements("item")
 select new
 {
     Link = "link: " + (string)node.Element("link"),
     Title = "Title: "+ (string)node.Element("title"),
     UpdatedOn ="Updated on : "+ (string)node.Element(atom + "updated"),
     Content = "Vacancy : " + (string)node.Element(a10+"content")
                                          .Element(ns+"Vacancy")
                                          .Element(ns+"ApplicationUrl")
 })