SelectSingleNode does not find node

Bogdan9119
Member Posts: 14
Hi! Straight to my problem...
In the following lines the call to selectSingleNode throws NullReferenceException:
XmlDocResponse := XmlHttp.responseXML();
XmlDocResponse.selectSingleNode('BarcodePrintZplResult');
And this is my XML doc:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><BarcodePrintZplResponse xmlns="http://tempuri.org/"><BarcodePrintZplResult>Object reference not set to an instance of an object.</BarcodePrintZplResult></BarcodePrintZplResponse></soap:Body></soap:Envelope>
How do I properly select the result node without using a 'XmlNamespaceManager' variable?
In the following lines the call to selectSingleNode throws NullReferenceException:
XmlDocResponse := XmlHttp.responseXML();
XmlDocResponse.selectSingleNode('BarcodePrintZplResult');
And this is my XML doc:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><BarcodePrintZplResponse xmlns="http://tempuri.org/"><BarcodePrintZplResult>Object reference not set to an instance of an object.</BarcodePrintZplResult></BarcodePrintZplResponse></soap:Body></soap:Envelope>
How do I properly select the result node without using a 'XmlNamespaceManager' variable?
0
Best Answer
-
SelectSingleNode expects an XPath expression, which indicates the path to the node that you want to select. Simply specifying the name of the node doesn't indicate path. You can get around this in one of two ways...
First - by specifying the fully qualified path to the node you want to select. i.e. //soap:Envelope/soap:Body/BarcodePrintZplResponse/BarcodePrintZplResult, or...
Second - by specifying a relative path to the node. One way might be to use //BarcodePrintZplResult as your XPath query.
Those two things being said - the XML document you posted doesn't pass my validator. The semicolons between the xmlns declarations are causing it to fail. I had to remove them to get the document to pass. I also had to remove the xmlns declaration on the BarcodePrintZplResponse node to get the Xpath expression to return results. Not sure why that namespace is declared there.
After making those minor edits, though, the following XML document returned the desired result when using the xpath expression //BarcodePrintZplResult<?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <soap:Body> <BarcodePrintZplResponse> <BarcodePrintZplResult>Object reference not set to an instance of an object.</BarcodePrintZplResult> </BarcodePrintZplResponse> </soap:Body> </soap:Envelope>
5
Answers
-
SelectSingleNode expects an XPath expression, which indicates the path to the node that you want to select. Simply specifying the name of the node doesn't indicate path. You can get around this in one of two ways...
First - by specifying the fully qualified path to the node you want to select. i.e. //soap:Envelope/soap:Body/BarcodePrintZplResponse/BarcodePrintZplResult, or...
Second - by specifying a relative path to the node. One way might be to use //BarcodePrintZplResult as your XPath query.
Those two things being said - the XML document you posted doesn't pass my validator. The semicolons between the xmlns declarations are causing it to fail. I had to remove them to get the document to pass. I also had to remove the xmlns declaration on the BarcodePrintZplResponse node to get the Xpath expression to return results. Not sure why that namespace is declared there.
After making those minor edits, though, the following XML document returned the desired result when using the xpath expression //BarcodePrintZplResult<?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <soap:Body> <BarcodePrintZplResponse> <BarcodePrintZplResult>Object reference not set to an instance of an object.</BarcodePrintZplResult> </BarcodePrintZplResponse> </soap:Body> </soap:Envelope>
5 -
Thanks for your help.
When I call XmlDocResponse.selectNodes('BarcodePrintZplResult') there is no NullReferenceException.0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions