0

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.

1

2 Answers 2

1

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:

Sign up to request clarification or add additional context in comments.

4 Comments

I see, I have to add ns: to the front of each node to tell xpath that the node i am looking for is in the namespace i specified. I'll give it a shot. Thanks.
@Shawn - Unless I'm missing something, you're binding ns to http://schemas.microsoft.com/taxonomy/2003/1, but your document uses the http://schemas.microsoft.com/support/2003/02/config namespace.
My namespace was wrong. That was the problem. Respect the namespace! Thank you much.
Good point. I will keep this in mind. I've accepted lwburks answer, and will do so for all my post from here on. Thanks.
1

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

The select statement should be:

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

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.