Working with multiple namespaces

D

daz_oldham

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.

As you may have calculated by my other posts, I am working under a lot
of new technologies that I didn't realise I would be when I took the
job - so I am on a steep learning curve (too steep at times!!! lol).

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"
xmlns="http://www.testurl.com/abc/2002/09">

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

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

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
instance?

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

Darren
 
M

Marc Gravell

The first param to SelectSingleNode should be in xpath - just with prefixes
e.g. using your default namespace.

Consider the following:

XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("x", doc.DocumentElement.GetAttribute("xmlns"));
XmlElement el =
(XmlElement)doc.DocumentElement.SelectSingleNode("x:Something/x:SomethingElse",
nsmgr);

This creates a new manager (using the existing name table), adds "x" as the
default namespace (reading from the document element), then queries
Something/SomethingElse in this namespace.

Hope this helps,

Marc
 
D

daz_oldham

Marc

What is the NameTable - because currently although this is in my
declaration, I'm not actually doing anything with it.

Do I need to do doc.Schemas.Add(...) for the specified xsd's as I
mentioned above?

Many thanks

Darren
 
D

daz_oldham

I got to the bottom of it Mark - it cost me 500 points on experts
exchange, but I'm not complaining.... I had the Namespace named
incorrectly

[QUTOTE]

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 "http://www.testurl.com/abc/2002/09"
not "ABC_ReturnAvailRS.xsd". So, when adding it to the namespace
manager it should look like:

nsmRequest.AddNamespace("ns1",
"http://www.testurl.com/abc/2002/09");

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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top