Multiple Namespaces / XPath with C#

Posted on 2006-05-23
Last Modified: 2013-11-19
Hi Everyone

I was wondering if anyone knew of a good guide to working with multiple
namespaces under .NET?  And also help me with my latest poser.

Very basically, I want to be able to do some XPath on XML I am getting
back from a web-service, and I have worked out that I need to apply my
DTDs because there is an xmlns attribute in my root element.

<Root Target="test" Version="2002A"

I have seen how to do this with an XmlNamespaceManager, but when I do
something like:

XmlNode node = xdHotels.SelectSingleNode("//ns1:ABC_ReturnAvailRS.xsd",

I just get a null value.

I have 'invented' that prefix because I cannot see a named prefix in my
xsd - but I have also tried String.Empty() and get null back.

The one thing I may be doing wrong is that in my ABC_ReturnAvailRS.xsd
there is:

<xs:include schemaLocation="ABC_CommonTypes.xsd"/>

And in this file there is:

<xs:include schemaLocation="ABC_SimpleTypes.xsd"/>

So does this mean I have to add these to my XmlNamespaceManager
instance?  And what do I do about giving each namespace a named

Hopefully I have made some sense here - and as ever I thank you for
taking the time to cast your eye over this for me.

Many many thanks

Best regards


Question by:daz_oldham
    LVL 52

    Accepted Solution

    This line looks wrong:

        nsmRequest.AddNamespace("ns1", "http://localhost/ABC_ReturnAvailRS.xsd");
        XmlNode node = xdHotels.SelectSingleNode("//ns1:ABC_ReturnAvailRS.xsd", nsmRequest);

    The namespace in your Root node is "" not "ABC_ReturnAvailRS.xsd". So, when adding it to the namespace manager it should look like:

        nsmRequest.AddNamespace("ns1", "");

    You then specify "ns1" as the prefix when searching for nodes in the XML:

        XmlNode node = xdHotels.SelectSingleNode("//ns1:nameofyournode", nsmRequest);

    I'm not quite sure why you have the name of the xsd in your XPath, but without seeing more of the XML its a bit difficult to be sure.

    Author Comment


    You have just dragged me from the depths of despair!   Thankyou so so much!!!!!!!!!

    Can you reccomend a good resource where I can read up on things like this?  I did buy Professional ASP.NET 2.0 XML by Wrox, but it is currently serving as a stand for my monitor - it is the best use of it!

    I did modify my code slightly to say:

    xdHotels.Schemas.Add(null, oAccom.Config.sXSDLocation + "ABC_AccomAvailRS.xsd");

    Is this worth doing, and should I also do it for the other XSD's that this includes as I mentioned in the original post?   Also, will doing this automatically check the XML is good, or do I have to call xdHotels.Schemas.Compile?

    Again, many thanks


    P.S. I am not sure if you can leave a response if I accept your answer, so I will wait for your reply before I do so.

    Many thanks

    LVL 52

    Expert Comment

    by:Carl Tawn
    There are some quick tutorials on XML in .Net here:

    There are also some samples on there about validating documents using Schemas and also using Schemas to create XML documents.

    In response to your P.S.; It is possible to still post comments to a question that has had an answer accepted and has ben closed.

    Author Comment

    Thanks again Carl - I wish I was brainy sometimes ;)

    If you don't mind me asking one last question, if my XML was:

    <ABC_AccomAvailRS Target="test" Version="2002A" xmlns="" xmlns:xsi="">
          <SessionInfo AnotherID="abcde" TextValue="ExpertsExchange"/>

    And I want to get the value of the attribute AnotherID, I can get the following to work...

    XmlNode node = xdHotels.SelectSingleNode("//ns1:SessionInfo", nsmRequest);
    String myString = node.Attributes[0].Value;

    However, I would ideally like to do something like:

    String myString = node.Attributes["AnotherID"].Value;

    This gives me a null exception - why?

    Many thanks

    LVL 52

    Expert Comment

    by:Carl Tawn
    You need to use the InnerText property:

        string myString = node.Attributes["AnotherID"].InnerText;

    Author Comment

    Thanks Carl

    Was just about to post to say I had got this to work:


    But I will keep this in mind for if I get the same problem cropping up.

    Do I not have to put "ns1:AnotherID" becaue I have loaded an element into an XmlNode object?

    Many thanks - if I had any points left, they'd be yours!

    LVL 52

    Expert Comment

    by:Carl Tawn
    No, namespaces only apply to nodes, not to attributes so you don't need the prefix.

    Author Comment

    Ah - I see.

    Thanks for the help - the past two weeks have been a crash course in me for C#, OOP. XML and XSD.  Next is XSLT - not done it for years, so I expect there will be more questions being posted at some point! ;)

    Best regards


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Suggested Solutions

    Most of the sites are being standardized with W3C Web Standards. W3C provides lot of web standard services to the web. They have the web specification, process and documentation for all the web standards. You can apply HTML, CSS and Accessibility st…
    JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
    Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now