Xpath with Namespace

tknight

Member
Joined
Sep 20, 2010
Messages
7
Programming Experience
Beginner
Hi - I have an XML file which is formatted like so:

HTML:
<result>
  <ResultExDataset xmlns="http://www.mysite/Results.xsd">
    <ResultSummary>
      <result>OK</result>
      <lasterrtype>None</lasterrtype>
      <lasterrtypevalue>0</lasterrtypevalue>
      <errordesc />
      <totalrows>0</totalrows>
      <morerowsavailable>False</morerowsavailable>
    </ResultSummary>
  </ResultExDataset>
  <TransactionsDataset xmlns="http://www.mysite/Transactions.xsd" />
</result>

I am trying to get to the "totalrows" element and after a bit of digging I found out that because namespaces are involved I can't just use xpath to get there without using namespacemanager. I've read up what I can but I still can't quite get to the little bugger so can anyone please steer me in the right direction. Here's what I have:

VB.NET:
Dim XMLdoc As New XmlDocument()
XMLdoc.Load("C:\Trans.xml")

Dim nsMgr As Xml.XmlNamespaceManager
nsMgr = New Xml.XmlNamespaceManager(XMLdoc.NameTable)
nsMgr.AddNamespace("RES", "http://www.mysite/Results.xsd")
nsMgr.AddNamespace("TRANS", "http://www.mysite/Transactions.xsd")
MsgBox(XMLdoc.SelectSingleNode("RES:ResultExDataset/RES:ResultSummary/RES:totalrows", nsMgr).InnerText)

Thanks,

Tom
 
Last edited:
The namespaces you have defined with the manager doesn't correspond to the namespaces in document. Also, the root element in document is 'result' and not 'ResultExDataset', so you can qualify that or do relative xpath from //.
 
Sorry - I changed the namespaces in the XML for privacy but forgot to change them in the code. I have edited the post. If they match what would the xpath string be to navigate to totalrows?

Thanks,

Tom
 
Back
Top