Link to home
Start Free TrialLog in
Avatar of maaknplan
maaknplanFlag for South Africa

asked on

XML Document XPath with Namespaces

Hi All

I have the following XML which I have received from the SharePoint Online Excel Services Rest API atom feed.

<?xml version="1.0" encoding="utf-8"?>
<entry xmlns:x="http://schemas.microsoft.com/office/2008/07/excelservices/rest" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservice" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
  <title type="text">'Foreign Currency Master Invoice'!Currency</title>
  <id>https://ltftradefinance.sharepoint.com/sites/Developer/_vti_bin/ExcelRest.aspx/Master%20Invoice%20Library/MasterTemplate.xlsx/Model/Ranges('%27%27Foreign%20Currency%20Master%20Invoice%27%27!Currency')</id>
  <updated>2017-02-06T06:10:00Z</updated>
  <author>
    <name />
  </author>
  <link rel="self" href="https://ltftradefinance.sharepoint.com/sites/Developer/_vti_bin/ExcelRest.aspx/Master%20Invoice%20Library/MasterTemplate.xlsx/Model/Ranges('%27%27Foreign%20Currency%20Master%20Invoice%27%27!Currency')?$format=atom" title="'Foreign Currency Master Invoice'!Currency" />
  <category term="ExcelServices.Range" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
  <content type="application/xml">
    <x:range name="'Foreign Currency Master Invoice'!Currency">
      <x:row>
        <x:c>
          <x:fv>USD</x:fv>
        </x:c>
      </x:row>
    </x:range>
  </content>
</entry>

Open in new window


I am trying to home in on the <x:range> node using XPath, but I simply cannot work out the XPath syntax.

My code is below and I would expect this to return the range node.

XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(xml);

            var names = new XmlNamespaceManager(xmlDoc.NameTable);
            names.AddNamespace("x", "http://schemas.microsoft.com/office/2008/07/excelservices/rest");
            names.AddNamespace("d", "http://schemas.microsoft.com/ado/2007/08/dataservice");
            names.AddNamespace("m", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");
            names.AddNamespace(string.Empty, "http://www.w3.org/2005/Atom");

            string xpath = @"@x:range";

            XmlNodeList nodes = xmlDoc.DocumentElement.SelectNodes(xpath, names);

Open in new window


Unfortunately this returns no nodes. The only way I have gotten this to work is to recurse through all the nodes until I hit the one I want - not too efficient.
ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of maaknplan

ASKER

As easy as that :-). Thanks for the help that works perfectly.
welcome,
well given that you had set up the namespace manager correctly, you left the easy part for me ;-)