Parsing an XmlDocument with a namespace

H

hharry

Hello All,

I have the following xml file: which I read into a string (sXML)

<?xml version="1.0" ?>
- <BackgroundReports xmlns="http://ns.hr-xml.org/2004-08-02" userId=""
password="">
- <BackgroundReportPackage type="report">
- <ProviderReferenceId>
<IdValue>7173511</IdValue>
</ProviderReferenceId>
</BackgroundReportPackage>
</BackgroundReports>

I am trying to use the xmldocument.selectsinglenode function to extract
node values

e.g.

Dim objXmlDoc As New XmlDocument
Dim objXmlNode As XmlNode
objXmlDoc.LoadXml(sXML)
objXmlNode = objXmlDoc.SelectSingleNode("//BackgroundReportPackage")

If I remove the xmlns spec, this code works fine...how do I use
SelectSingleNode in conjunction with a namespace ?


Thanks In Advance
 
H

hharry

Solved it:

Use the XmlNameSpaceManager object

Dim nsmgr As XmlNamespaceManager = New
XmlNamespaceManager(objXmlDoc.NameTable)
nsmgr.AddNamespace(String.Empty, "http://ns.hr-xml.org/2004-08-02")
objXmlNode = objXmlDoc.SelectSingleNode("//BackgroundReportPackage")
 
H

hharry

Code correction:

nsmgr = New XmlNamespaceManager(objXmlDoc.NameTable)
nsmgr.AddNamespace("ns1", "http://ns.hr-xml.org/2004-08-02")
Dim root As XmlElement = objXmlDoc.DocumentElement
oNode = root.SelectSingleNode("//ns1:BackgroundReportPackage", nsmgr)
 

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