NOCARRIER
NOCARRIER

Reputation: 2635

XPATH Help: Finding XML Nodes in a Namespace with XPathNodeIterator

I am having problems finding the proper XPATH syntax to get the nodes I want with this XML file. For one, there is no NameSPace in the XML so I have to add one in code. I think this effects my XPATH.

I have an XML file that looks like this:

<configuration xmlns="http://schemas.microsoft.com/support/2003/02/config">
  <name>Content</name>
  <description>Desc</description>
  <lastModifiedBy>Me</lastModifiedBy>
  <lastModifiedDate>2011-04-18T14:05:00</lastModifiedDate>
  <section name="MaintenanceNotices">
    <key name="MaintenanceNote1" value="The Title Value" />
    <link id="1234" type="5" href="" target="_self" name="MaintenanceNote1a">
      <description>Description</description>
    </link>
  </section>
</configuration>

So, for the XPATH if I want to get the "NAme" NODE value in the "CONFIGURATION" element, I assume I would use this XPATH:

/configuration/name

but sometimes I need to append ns: to it:

/ns:configuration/ns:name

And then I can find the element value like so:

while (xmlNodeIterator.MoveNext()) {
    result += xmlNodeIterator.Current.SelectSingleNode("name", nsmgr).ToString();
}

But this is not working for me at all. It wont find any values in the XML no matter what xpath i try. Here is my code:

    private string GetXML()
    {
        string result = string.Empty;
        string fileName = "Content.xml";
        string filePath = "C:\\Content\\{0}";

        XPathDocument xdoc = new XPathDocument(string.Format(filePath, fileName));

        var nav = xdoc.CreateNavigator();

        XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable());
        nsmgr.AddNamespace("ns", "http://schemas.microsoft.com/taxonomy/2003/1");
        nsmgr.AddNamespace("gds", "http://support.microsoft.com/common/schemas/gdsPage/1/");

        string sectionName = "MaintenanceNotices";
        string xpath = "/configuration"; 

        //  section[name={0}]"; ///key[name=MaintenanceNote1]/";

        XPathNodeIterator xmlNodeIterator = nav.Select(string.Format(xpath, sectionName), nsmgr);

        while (xmlNodeIterator.MoveNext())
        {
            result += xmlNodeIterator.Current.SelectSingleNode("name", nsmgr).ToString();
        }

        return result;
    }

Can you see any problems, or offer a suggestion on my Xpath Syntax?

Thanks.

Upvotes: 0

Views: 1008

Answers (2)

Richard Schneider
Richard Schneider

Reputation: 35477

As @lwburk said, you need to register the namespace.

The select statement should be:

var xmlNodeIterator = nav.Select("/ns:configuration/ns:name", nsmgr)

Upvotes: 1

Wayne
Wayne

Reputation: 60414

I don't see anything wrong with your XPath, except that you never register the namespace that your document's nodes are in:

"http://schemas.microsoft.com/support/2003/02/config"

It looks like you already know how to do that in your language (using AddNamespace), so maybe it's just an oversight. If you need more information, this should help:

Upvotes: 1

Related Questions