XPath and XML Namespaces

It’s great that in C# code you can easily reference XML nodes using the XmlDocument’s built-in XPath support:

1
XmlNode titleNode = doc.SelectSingleNode("/html/head/title");

But have you ever run into the situation where you call SelectSingleNode or SelectNodes and you get back nothing when you can see there are nodes there? The problem might be that the document is using XML Namespaces.

Consider this XML snippet:

1
2
3
4
5
<urlset xmlns="http://www.google.com/schemas/sitemap/0.84">
   <url>
      <loc>http://blog.stevex.net/</loc>
   </url>
</urlset>

If you attempt to retrieve nodes from this document using this code:

1
XmlNode urlNodes = doc.SelectNodes("/urlset/url");

You might expect to get back a set of all the url nodes, but instead you get back nothing.

To query into a document that uses namespaces, you need to use an XmlNamespaceManager. The namespace manager takes care of mapping a prefix to a namespace, and lets you associate the nodes in the query with particular namespaces.

The following code would query the sitemap.xml:

1
2
3
XmlNamespaceManager nsMgr = new XmlNamespaceManager(myXmlDoc.NameTable);
nsMgr.AddNamespace("sm", "http://www.google.com/schemas/sitemap/0.84");
XmlNode urlNodes = doc.SelectNodes("/sm:urlset/sm:url");

If a page uses multiple namespaces then you’d have multiple calls to AddNamespace with different prefixes. Your query string can include nodes from different namespaces.

16 Responses to “XPath and XML Namespaces”

  1. Martin Says:

    Thank you!! I have been struggling with this for a couple of hours and now (at last!) it is working!

  2. Makarad Says:

    I am having similar problem but by xml doc is having 2 namespace attributes as given below

    What should be XmlNamespaceManager for this one?

    Thanks
    MAKARAND

  3. SvendTofte Says:

    This was just what I needed to know. I have a document similar to this one, the documentation claims you can use “String.Empty”, for a default namespace (and thus, I assumed not need prefixes in the actual xpath expression).

    Thanks, was just about to waste another hour on this non-issue ;)

  4. Christian Palm Says:

    I think you have a typo in your example

    You write:
    XmlNamespaceManager nsMgr = new XmlNamespaceManager(myXmlDoc.NameTable);
    It shoud be, I think:
    XmlNamespaceManager nsMgr = new XmlNamespaceManager(doc.NameTable);

  5. Brad Candell Says:

    Thank you SO MUCH.. I was about to give up all together on these stupid Namespaces! I found your document and had my code working within mere minutes..

    Thanks again!

    -Brad

  6. Lee Says:

    THANK YOU THANK YOU THANK YOU THANK YOU. You saved me from /wrists. I’ve been looking and looking at my XPath and KNEW it was right. I had no idea that the namespace would muck things up.

    Thanks again,
    Lee

  7. XML, XPath and XML Namespaces oh my… | The Sexy Gorilla Says:

    [...] was just about to /mywrists today when I found this post. I wanted to post to this blog to say thanks to SteveX for posting [...]

  8. joey Says:

    thanks for the info:

    my code required a different implementation…
    XmlNodeList urlLocNode = sitemap.SelectNodes(“/sm:urlset/sm:url/sm:loc”, nsMgr);

    1) SelectNodes returns a XmlNodeList not an XmlNode

    2) You need to use the overloaded version of the SelectNodes method to pass the XmlNamespaceManager object.

  9. XML, XPath and XML Namespaces oh my… « Everything I do Makes Me a Bitweiser Says:

    [...] XPath and XML Namespaces oh my… I was just about to /mywrists today when I found this post. I wanted to post to this blog to say thanks to SteveX for posting [...]

  10. suriya Says:

    Thanks Steve. This post is REALLY very useful. I was struggling to get xmlnode by using xpath from last two days and i got this post . Thanks once again.

  11. Corey Broderick Says:

    I thank you. My coworkers thank you. The cabinet I’ve been banging my head against for the last 2 hours thanks you. Cheers.

  12. chrisb Says:

    I only wish we could use the empty string for the namespace prefix like everyone else’s libraries use. It’s no fun rewriting tons of xpath to be compatible with Microsoft libraries. This took me days to figure out, and I found a MSFT reference page going back to ’03 where they realize it’s wrong (“by design”) and I guess they’ll never make their libraries work the same as everyone else’s. I love their tools but things like this really chafe.

  13. Farschidus Says:

    Thank you, It’s a great help for me.
    I think you forgot to pass XmlNamespaceManager(nsMgr) to SelectNodes method:

    XmlNode urlNodes = doc.SelectNodes(“/sm:urlset/sm:url”, nsMgr);

    Thanks again.

  14. Localle Says:

    yes always remember to pass the Namespace manager as the other parameter and it works.
    XmlNode n = xmldoc.SelectNodes(“sm:urlset/sm:url,nsMgr);

  15. Roy Says:

    THANK YOU !! THANK YOU !!

    I thought my xml was broken or some shit like that… I’m glad I stumbled upon this !!

  16. Zoki Kocev Says:

    If you are struggling with xml namespaces, there is a great tutorial on xpath namespaces at xml reports. It walks you through it in very simple steps xml reports

Leave a Reply

buy cheap Endep online buy cheapest Endep and Endep how to order Amitriptyline online without prescription buy Amitriptyline without rx from us pharmacy online Amitriptyline purchase purchase online prescription Amitriptyline without purchasing Amitriptyline online without prescription order generic Amitriptyline buy Endep no prescriptions how to order Endep online without prescription comprare Endep generico Endep online buy Endep in england buy cheap Endep online free consult Endep purchased online without prescription what is Endep Endep tabletten buy Endep no perscription cod Endep non rx fedex overnight free want to buy Anafranil in usa Aricept overdose buy discount Aricept on line Aricept buy Aricept online Aricept order buy Aricept pills Nizoral suppliers cheap order rx Nizoral canada Nizoral order Nizoral usa cod comprar Nizoral generico buy Nizoral 200 mg buy cheap Nizoral under without rx buy cheap Nizoral with dr. prescription Prozac 200 mg buy Lisinopril australia el Nizoral generico buy Lisinopril tablets without rx next day delivery Lisinopril with no script buy Lisinopril c o d purchase arimidex no scams order Arimidex order amex Arimidex canadian pharmacy buy Arimidex without doctor order rx free Arimidex buy Abilify with american express buy Abilify cod where can i purchase Abilify without a prescription order Finpecia without a prescription cheap order rx Finpecia purchase Famvir no visa online without prescription buy Famvir paypal without rx buy Famvir without Famvir without prescription medications Famvir without persription purchase Famvir online no membership overnight shipping Famvir no physician approval Amitriptyline purchase order Amitriptyline free next day airAmitriptyline on line buy next day Fluoxetine buy Fluoxetine line purchase Fluoxetine cod delivery buy Fluoxetine 20 mg purchase Fluoxetine buy cheap Fluoxetine under without rx purchase Fluoxetine visa without prescription Fluoxetine by mail Fluoxetine for pets Fluoxetine 10mg Amitriptyline purchase online how to order Famvir online without a rx Prozac Amitriptyline no perscription Famvir next day purchase Anafranil no prescription cheap buy cheapest Anafranil next day delivery on Anafranil saturday Anafranil buy Anafranil buy Anafranil overnight where to purchase Anafranil no prescription no fees Anafranil drug buy Lisinopril where Lisinopril buy on line order Lisinopril online no membership overnight shipping no prescription required for Lisinopril order maxalt cash on delivery Maxalt from canada buy cheapest Maxalt purchase Maxalt visa without prescription buy Maxalt uk Maxalt 10 mg purchase Maxalt no visa online without prescription purchase generic Lisinopril online Lisinopril no prescription to buy maxalt order online no membership overnight how to get a arimidex rx Aricept without a perscription fedex Aricept overnight without a prescription Donepezil prescription order buy genuine Aricept Aricept uk sales buy Amitriptyline with amex Accutane 40 mg delivered overnight buy brand Accutane 40 mg Accutane 40 mg orderd online without prescription where to purchase generic Accutane 40 mg online without a rx how to get 40 mg Accutane without next day delivery 40 mg Accutane with no script buy Amitriptyline online Accutane 40 mg online no prescription buy Accutane 40 mg online us pharmacy Accutane 40 mg best buy buy Accutane 40 mg c o d order 40 mg Accutane cash on delivery no presciption 40 mg Accutane