Search code examples
c#.netxmlatom-feed

Read Atom Feed by XPATH in .NET


The problem is that the code couldn't read Atom feed xml format like this URL.

I have no idea what is wrong in the code, but the code doesn't has any issue with RSS version 2.0 and every thing are good

There is C# code simple I have used to read rss feed and atom

if have any solution I will appreciate

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";
  }
}

Solution

  • The feed tag is in a namespace and defines the default namespace for the document. You need to set up a NameSpaceManager and use that one in SelectSingleNode().

    Something like

    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);
    

    A problem is that the feed is invalid XML. The embedded IFrame in line 188 (at the time of writing) uses HTML syntax which is invalid in XML.

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

    In XML, there cannot be an attribute allowfullscreen without a value. IMHO the website should use <![CDATA[...]]> to insert the HTML.