Link to home
Start Free TrialLog in
Avatar of daz_oldham
daz_oldham

asked on

Multiple Namespaces / XPath with C#

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"
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


ASKER CERTIFIED SOLUTION
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of daz_oldham
daz_oldham

ASKER

Carl,

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

Darren

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

Darren
There are some quick tutorials on XML in .Net here:

    http://dotnetjunkies.com/QuickStartv20/howto/doc/Xml/MultipleXmlReader.aspx


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.
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="http://www.testurl.com/abc/2002/09" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <SessionInfo AnotherID="abcde" TextValue="ExpertsExchange"/>
</ABC_AccomAvailRS>

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

Darren
You need to use the InnerText property:

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

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

xnSession.Attributes["AnotherID"].Value;

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!

Darren
No, namespaces only apply to nodes, not to attributes so you don't need the prefix.
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

Darren