在 .NET 中通过 XPATH 读取 Atom Feed
Read Atom Feed by XPATH in .NET
问题是代码无法读取 Atom 提要 xml 格式,如 this URL。
我不知道代码中有什么问题,但代码对 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";
}
}
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);
一个问题是 Feed 无效 XML。第 188 行的嵌入式 IFrame(在撰写本文时)使用 HTML 语法,这在 XML.
中无效
<iframe [...] allowfullscreen></iframe>
在 XML 中,不能有没有值的属性 allowfullscreen
。恕我直言,该网站应使用 <![CDATA[...]]>
插入 HTML.
问题是代码无法读取 Atom 提要 xml 格式,如 this URL。
我不知道代码中有什么问题,但代码对 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";
}
}
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);
一个问题是 Feed 无效 XML。第 188 行的嵌入式 IFrame(在撰写本文时)使用 HTML 语法,这在 XML.
中无效<iframe [...] allowfullscreen></iframe>
在 XML 中,不能有没有值的属性 allowfullscreen
。恕我直言,该网站应使用 <![CDATA[...]]>
插入 HTML.