Reaching nodes

davearia

Well-known member
Joined
Jan 4, 2005
Messages
184
Hi All,

Hope your weekend is good.

Here is some XML:
Code:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <placeOrder xmlns="http://tempuri.org/">
      <dbsAPIRequest>
        <Header>
          <loginGUID>026b518c-6916-434a-85ba-88fedce62fac</loginGUID>
          <API_Version>1</API_Version>
        </Header>
      </dbsAPIRequest>
    </placeOrder>
  </soap:Body>
</soap:Envelope>
Here is part of my code that is trying to reach elements within the header node:
Code:
 Dim HeaderXMLNodelst As XmlNodeList = orders.SelectNodes("//Envelope/Body/placeOrder/dabsAPIRequest/Header")
For Each HeaderXMLItem As XmlNode In HeaderXMLNodelst
     loginGUID = HeaderXMLItem.SelectSingleNode("CompanyGUID").InnerXml
     API_Version = Convert.ToInt32(HeaderXMLItem.SelectSingleNode("API_Version").InnerXml)
Next

The code does not error but fails to bring any values for loginGUID and API_Version .

Any ideas?

Cheers, Dave.
 
Namespaces in XPath

Okay, firstly your XML contains the element dbsAPIRequest but your XPath contains dabsAPIRequest. Note the extra a.

Besides that, namespaces must be declared in the XPath and then issued with a prefix. In this case your XPath might look something like:

Code:
"//s:Envelope/s:Body/placeOrder/dbsAPIRequest/Header s http://schemas.xmlsoap.org/soap/envelope/"

Here we define the prefix s to indicate the namespace http://schemas.xmlsoap.org/soap/envelope and prefix Envelope and Body with s. Note that the prefix used in the XPath need not match the prefix used in the XML.

Good luck :cool:
 
Back
Top