?
Solved

Multiple Namespaces / XPath with C#

Posted on 2006-05-23
8
Medium Priority
?
672 Views
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"
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


0
Comment
Question by:daz_oldham
  • 4
  • 4
8 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 2000 total points
ID: 16741804
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);

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.
0
 

Author Comment

by:daz_oldham
ID: 16741974
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
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 16742047
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.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:daz_oldham
ID: 16742262
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
0
 
LVL 52

Expert Comment

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

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

Author Comment

by:daz_oldham
ID: 16742398
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
0
 
LVL 52

Expert Comment

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

Author Comment

by:daz_oldham
ID: 16742461
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
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

840 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