0

I'm trying to get all the entry elements so I can display them, haven't done Xpath for a while but I thought it would be fairly simple heres what I have so far - rssNodes count is 0, what am I missing?

XmlDocument rssXmlDoc = new XmlDocument();
rssXmlDoc.Load("http://www.businessopportunities.ukti.gov.uk/alertfeed/1425362.rss");

var rssNodes = rssXmlDoc.SelectNodes("feed/entry");

The XML file has the following structure:

<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <!-- some other child elements -->
  <entry>
    <!-- child elements -->
  </entry>
  <entry>
    <!-- child elements -->
  </entry>
  <!-- more entry elements -->
  <!-- some other child elements -->
</feed>
helb
  • 7,609
  • 8
  • 36
  • 58
Ayo Adesina
  • 2,231
  • 3
  • 37
  • 71
  • Can you show your XML? – rory.ap Apr 02 '15 at 11:42
  • Follow the link > http://www.businessopportunities.ukti.gov.uk/alertfeed/1425362.rss – Ayo Adesina Apr 02 '15 at 11:44
  • possible duplicate of [which xpath expression will allow me to select these nodes?](http://stackoverflow.com/questions/14370989/which-xpath-expression-will-allow-me-to-select-these-nodes) – JLRishe Apr 02 '15 at 11:57
  • I updated your question title to narrow down the problem. Also, I took the liberty of including the essential parts of the XML file. – helb Apr 02 '15 at 12:46
  • @helb thank you, if you can you could look at the last part for me too :-) http://stackoverflow.com/questions/29413435/using-xpath-to-get-element-from-xmlnode – Ayo Adesina Apr 02 '15 at 12:52

2 Answers2

3

You need to properly use namespaces:

var nsm = new XmlNamespaceManager(rssXmlDoc.NameTable);
nsm.AddNamespace("atom", "http://www.w3.org/2005/Atom");

var entries = rssXmlDoc.SelectNodes("/atom:feed/atom:entry", nsm);
JLRishe
  • 99,490
  • 19
  • 131
  • 169
0

You need to respect XML namespaces with XPath.

NOTE: If the namespace of the entry element is known, see JLRishe's answer which is more elegant in that case.

If you don't know the XML namespace beforehand you can also ignore it using the XPath built-in local-name() function:

SelectNodes("//*[local-name()='entry']") 

This will get all entry elements in the entire XML document, no matter which namespace the element belongs to.

helb
  • 7,609
  • 8
  • 36
  • 58