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("n s1",
"http://localhost/ABC_ReturnAvailRS.xsd");
XmlNode node = xdHotels.SelectSingleNode( "//ns1:ABC _ReturnAva ilRS.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_Common Types.xsd" />
And in this file there is:
<xs:include schemaLocation="ABC_Simple Types.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
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("n
"http://localhost/ABC_ReturnAvailRS.xsd");
XmlNode node = xdHotels.SelectSingleNode(
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_Common
And in this file there is:
<xs:include schemaLocation="ABC_Simple
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
ASKER
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:Ses sionInfo", 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
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(
String myString = node.Attributes[0].Value;
However, I would ideally like to do something like:
String myString = node.Attributes["AnotherID
This gives me a null exception - why?
Many thanks
Darren
You need to use the InnerText property:
string myString = node.Attributes["AnotherID "].InnerTe xt;
string myString = node.Attributes["AnotherID
ASKER
Thanks Carl
Was just about to post to say I had got this to work:
xnSession.Attributes["Anot herID"].Va lue;
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
Was just about to post to say I had got this to work:
xnSession.Attributes["Anot
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.
ASKER
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
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
ASKER
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,
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