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>
5

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.