[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 321
  • Last Modified:

XMLDocument not able to locate a node - SelectSingleNode

I have simple XML file. I need to read the value of the element <totalresults>.
Please see code below, it never seems to locate that node. What is wrong with this?

Please help.
XML

<?xml version="1.0" encoding="utf-8"?>
<feed>
<totalresults>0</totalresults>
<startindex>1</startindex>
<itemsperpage>25</itemsperpage>
<entry/>
</feed>

C# Code
xmldoc.Load("C:/test.xml");
XmlNode node=xmldoc.SelectSingleNode("//totalresults");
if (node != null)
  return node.InnerText;
else
  return "0";

Open in new window

0
codemonkey2480
Asked:
codemonkey2480
1 Solution
 
codemonkey2480Author Commented:
Found the solution:

The C#/xpath below would grab totalresults element

Further reading: http://msdn.microsoft.com/en-us/library/fb63z0tw.aspx

XmlNode node = xmldoc.SelectSingleNode("descendant::feed").FirstChild;
            if (node != null)
                return node.InnerText;
            else
                return "0";

Open in new window

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now