Reading complex XML using XPath

A

aligns

Hi,

I want to read the below xml using XPath queries.

I am getting "object reference is nothing" output, though i think the
code is correct and it should get the object required.

Please help me out.

The XML and Code is below:

XML:

<?xml version="1.0" encoding="utf-8" ?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/
envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<dateSection xmlns="http://schemas.xmlsoap.com/50">
<ns1:dateBody xmlns:ns1="http://schemas.xmlsoap.com/60">
<ns1:dateState>
<ns1:stateCode>OK</ns1:stateCode>
</ns1:dateState>
<ns1:dateAction>
<ns1:action>http://huggisadventure.co.au/</
ns1:action>
</ns1:dateAction>
</ns1:dateBody>
</dateSection>
</soapenv:Body>
</soapenv:Envelope>

CODE:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not IsPostBack Then
langSettings = New System.Xml.XmlDocument
langNSManager = New
System.Xml.XmlNamespaceManager(langSettings.NameTable)

langSettings.Load("C:\\Inetpub\\wwwroot\\WebTest\\xml\
\test.xml")
langNSManager.AddNamespace("soapenv", "http://
schemas.xmlsoap.org/soap/envelope/")
langNSManager.AddNamespace("ns1", "http://
schemas.xmlsoap.com/60")

lblFName.Text =
langSettings.SelectSingleNode("soapenv:Envelope/soapenv:Body/
dateSection/ns1:dateBody/ns1:dateState/ns1:stateCode",
langNSManager).InnerText

End If
End Sub
 
M

Martin Honnen

aligns said:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/
envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<dateSection xmlns="http://schemas.xmlsoap.com/50">
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This element is in the namespace http://schemas.xmlsoap.com/50, to
select it with XPath 1.0 you need to bind a prefix to the namespace URI
and use that prefix. See below:
<ns1:dateBody xmlns:ns1="http://schemas.xmlsoap.com/60">
<ns1:dateState>
<ns1:stateCode>OK</ns1:stateCode>
</ns1:dateState>
<ns1:dateAction>
<ns1:action>http://huggisadventure.co.au/</
ns1:action>
</ns1:dateAction>
</ns1:dateBody>
</dateSection>
</soapenv:Body>
</soapenv:Envelope>

CODE:
langNSManager.AddNamespace("soapenv", "http://
schemas.xmlsoap.org/soap/envelope/")
langNSManager.AddNamespace("ns1", "http://
schemas.xmlsoap.com/60")

langNSManager.AddNamespace("df", "http://schemas.xmlsoap.com/50");
lblFName.Text =
langSettings.SelectSingleNode("soapenv:Envelope/soapenv:Body/
dateSection/ns1:dateBody/ns1:dateState/ns1:stateCode",
langNSManager).InnerText

lblFName.Text =
langSettings.SelectSingleNode("soapenv:Envelope/soapenv:Body/df:dateSection/ns1:dateBody/ns1:dateState/ns1:stateCode",
langNSManager).InnerText
 
M

Martin

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This element is in the namespacehttp://schemas.xmlsoap.com/50, to
select it with XPath 1.0 you need to bind a prefix to the namespace URI
and use that prefix. See below:







langNSManager.AddNamespace("df", "http://schemas.xmlsoap.com/50");


lblFName.Text =
langSettings.SelectSingleNode("soapenv:Envelope/soapenv:Body/df:dateSection-/ns1:dateBody/ns1:dateState/ns1:stateCode",
langNSManager).InnerText

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/- Hide quoted text -

- Show quoted text -

Thanks Martin, that worked like a charm.

I have another small problem.

What will be the query if the XML is as given below?
i tried using this query but its not working :(

"langSettings.SelectSingleNode("soapenv:Envelope/soapenv:Body/
df:dateSection-/dateBody/dateState/ns1:date/ns1:stateCode",
langNSManager).InnerText"


<?xml version="1.0" encoding="utf-8" ?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/
envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<dateSection xmlns="http://schemas.xmlsoap.com/50">
<dateBody>
<dateState>
<ns1:date xmlns:ns2="http://schemas.xmlsoap.com/
70">
<ns1:stateCode>OK</ns1:stateCode>
</ns1:date>
<dateState>
<dateAction>
<ns2:time xmlns:ns2="http://schemas.xmlsoap.com/
80">
<ns2:action>http://
huggisadventure.co.au/>
</<ns2:time>
</dateAction>
<dateBody>
</dateSection>
</soapenv:Body>
</soapenv:Envelope>
 
M

Martin Honnen

Martin said:
What will be the query if the XML is as given below?
i tried using this query but its not working :(

"langSettings.SelectSingleNode("soapenv:Envelope/soapenv:Body/
df:dateSection-/dateBody/dateState/ns1:date/ns1:stateCode",
langNSManager).InnerText"


<?xml version="1.0" encoding="utf-8" ?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/
envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<dateSection xmlns="http://schemas.xmlsoap.com/50">
<dateBody>
<dateState>

The default namespace declaration applies to the child elements as well
so you need

soapenv:Envelope/soapenv:Body/df:dateSection/df:dateBody/df:dateState/ns1:date/ns1:stateCode
 

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