NET中的XPATH读取Atom Feed

时间:2018-07-23 12:43:45

标签: c# .net xml atom-feed

问题在于代码无法读取this URL之类的Atom提要xml格式。

我不知道代码中有什么问题,但是该代码在RSS 2.0版中没有任何问题,一切都很好

我曾经使用过简单的C#代码来阅读rss feed和atom

如果有什么解决方案,我将感激

XmlDocumentxmlDoc=newXmlDocument();
xmlDoc.Load("http://wintermute.com.au/bits.atom");
XmlNodeListitemNodes=xmlDoc.SelectNodes("//feed/entry");
foreach(XmlNodeitemNodeinitemNodes)
{
  //TitleTag
  if(itemNode.SelectSingleNode("title")!=null)
  {
    titletag=itemNode.SelectSingleNode("title").InnerText;
  }
  else
  {
    titletag="Notitlefound";
  }
  //PubDateTag
  if(itemNode.SelectSingleNode("published")!=null)
  {
    pubDatetag=itemNode.SelectSingleNode("published").InnerText;
  }
  else
  {
    pubDatetag="Nopublishedfound";
  }
  //LinkTag
  if(itemNode.SelectSingleNode("link")!=null)
  {
    linktag=itemNode.SelectSingleNode("link").InnerText;
  }
  else
  {
    linktag="NopubDatefound";
  }
  //Logo,Image
  if(itemNode.SelectSingleNode("logo")!=null)
  {
    imgtag=itemNode.SelectSingleNode("logo").InnerText;
  }
  if(itemNode.SelectSingleNode("image")!=null)
  {
    imgtag=itemNode.SelectSingleNode("image").InnerText;
  }
  else
  {
    imgtag="NoImagefound";
  }
  //summaryTag
  if(itemNode.SelectSingleNode("summary")!=null)
  {
    destag=itemNode.SelectSingleNode("summary").InnerText;
  }
  else
  {
    destag="Nosummaryfound";
  }
}

1 个答案:

答案 0 :(得分:0)

feed标记位于名称空间中,并定义文档的默认名称空间。您需要设置一个NameSpaceManager并在SelectSingleNode()中使用它。

类似

var doc = new XmlDocument();
doc.Load(source);
var nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("f", "http://www.w3.org/2005/Atom");
var something= doc.SelectSingleNode("//f:feed/f:entry", nsmgr);

一个问题是提要是无效的XML。第188行中的嵌入式IFrame(在撰写本文时)使用的HTML语法在XML中无效。

<iframe [...] allowfullscreen></iframe>

在XML中,不能有没有值的属性allowfullscreen。恕我直言,网站应使用<![CDATA[...]]>插入HTML。