Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SelectSingleNode and NameSpaces???

Status
Not open for further replies.

clumsybiker

Programmer
Jul 18, 2005
2
GB
I have the following xml:

<NLIS_MESSAGE xmlns=" xmlns:xsi=" xsi:schemaLocation=" Acknowledgement.xsd">
<MESSAGE_ACKNOWLEDGEMENT>
<RESPONSE_MNEMONIC>ACKNOWLEDGE</RESPONSE_MNEMONIC>
<ERROR_MNEMONIC>SUCCESS</ERROR_MNEMONIC>
</MESSAGE_ACKNOWLEDGEMENT>
</NLIS_MESSAGE>

This xml is loaded into an xmlDocument object in VB .NET.
As you can see it has several namespaces. I am struggling to understand how I can do a document.SelectSingleNode to get hold of the values of the Mnemomic fields in the message, to determine whether "Acknowledge" and "Success" are present.

When I do:
dim oNode as xmlnode = Document.SelectSingleNode("/NLIS_MESSAGE/MESSAGE_ACKNOWLEDGEMENT/RESPONSE_MNEMONIC")
I get oNode = Nothing

I've tried with a namespacemanager but still get nothing, but I think I don't know enough about namespaces / namespacemanagers to get it right.

Can anyone help?

Thanks,
 
I believe you need to add a default namespace prefix and reference it when you do any XPath queries (like SelectSingleNode).

I had a similar problem and it was fixed by doing this. Try this:

Change xmlns="to xmlns:n="
And dim oNode as xmlnode = Document.SelectSingleNode("/NLIS_MESSAGE/MESSAGE_ACKNOWLEDGEMENT/RESPONSE_MNEMONIC")
to dim oNode as xmlnode = Document.SelectSingleNode("/n:NLIS_MESSAGE/n:MESSAGE_ACKNOWLEDGEMENT/n:RESPONSE_MNEMONIC")

And see if that works.

Also, I think you might not need the opening / before NLIS_MESSAGE, but I'm not sure about that.

Good luck!
 
Thanks FatMosh, but I don't have control over the source xml - it comes in from a data provider hence I can't simply make changes to the namespace declarations.

For those also stuggling with this problem, I have found a solution using the namespacemanager class - although not quite in the same way as the Microsoft Developer Network examples....

I've aliased the default namespace to "def" using a NameSpaceManager - and the result is I can do SelectSingleNode in the way fatmosh specified above:

so:

'Declare the NameSpaceManager
dim oNSMgr as new XmlNameSpaceManager(Document.NameTable)
'Alias the default namespace to "def"
oNSMGR.AddNameSpace("def", "'The node can now be selected via the NSManager, using the "def" alias
dim oNode as xmlnode = Document.SelectSingleNode("/def:NLIS_MESSAGE/def:MESSAGE_ACKNOWLEDGEMENT/def:ERROR_MNEMONIC", oNSMgr)
MsgBox(oNode.InnerText)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top