LilyRock
asked on
How to get to the desired node?
I have this xml file as below. I can't get the following codes to work. I tried to get to the bill node. I have search the knowledgebase but haven't found a solution. please help me. I am very new with XML. This is my first XML project. Thank you so much.
Dim doc As New Xml.XmlDocument
doc.Load("c:/temp/test.xml ")
Dim mgr As XmlNamespaceManager = New XmlNamespaceManager(doc.Na meTable)
mgr.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/")
mgr.AddNamespace("rs", "https://secure.abccompany.com/core/")
Dim nodelist As XmlNodeList = doc.SelectNodes("soap:Enve lope/soap: body/rs:Re priceStack /rs:stack/ ", mgr)
Dim node As XmlNode
For Each node In nodelist
Dim strg As String
strg = node.SelectSingleNode("rs: bills").In nerText
Next
----------------here is the xml file---------------
<?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>< RepriceSta ck xmlns="https://secure.abccompany.com/core">
<stack><testmode>true</tes tmode><cli entid>abcc ompany</cl ientid>
<bills>
<bill><billid>123</bilid>< desc>Post it</desc></bill>
<bill><billid>1234</billid ><desc>Not e pad</desc></bill>
</bills>
</stack></RepriceStack></s oap:Body>< /soap:Enve lope>
Dim doc As New Xml.XmlDocument
doc.Load("c:/temp/test.xml
Dim mgr As XmlNamespaceManager = New XmlNamespaceManager(doc.Na
mgr.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/")
mgr.AddNamespace("rs", "https://secure.abccompany.com/core/")
Dim nodelist As XmlNodeList = doc.SelectNodes("soap:Enve
Dim node As XmlNode
For Each node In nodelist
Dim strg As String
strg = node.SelectSingleNode("rs:
Next
----------------here is the xml file---------------
<?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><
<stack><testmode>true</tes
<bills>
<bill><billid>123</bilid><
<bill><billid>1234</billid
</bills>
</stack></RepriceStack></s
ASKER
Thanks for your prompt answer.
It's my typo. Either way, it still doesn't work.
It's my typo. Either way, it still doesn't work.
Other typos?
The '/' should not be there at the end of the XPath
XML is not wellformed 123</bilid> should have two ll's
The '/' should not be there at the end of the XPath
XML is not wellformed 123</bilid> should have two ll's
ASKER
I changed to this
Dim nodelist As XmlNodeList = doc.SelectNodes("soap:Enve lope/soap: body/rs:Re priceStack /rs:stack" , mgr)
It didn't give any error but why the nodelist.Count = 0?
Sorry for the typos. I misplaced my eye glass.
Dim nodelist As XmlNodeList = doc.SelectNodes("soap:Enve
It didn't give any error but why the nodelist.Count = 0?
Sorry for the typos. I misplaced my eye glass.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Yes. I had tried that too. Same issue, zero count
Did you try the obvious stuff, such as checking that the document is loaded correctly (by serialising the doc object to the output)
@insoftservice... wrong question?
ASKER
I changed my codes around and get to this point
Dim doc As New Xml.XmlDocument
doc.Load("c:/temp/R1ExCR20 1103311243 18.xml")
Dim mgr As XmlNamespaceManager = New XmlNamespaceManager(doc.Na meTable)
mgr.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/")
mgr.AddNamespace("rs", "https://secure.abccompany.com/core/")
' Dim nodelist As XmlNodeList = doc.SelectNodes("/soap:Env elope/soap :Body/rs:R epriceStac k/rs:stack /rs:bills" , mgr)
Dim envelope As XmlNode = doc.DocumentElement
Dim body As XmlNode = envelope.SelectSingleNode( "//soap:Bo dy", mgr)
Dim resp As XmlNode = body.SelectSingleNode("//r s:RepriceS tack", mgr)
Dim sk As XmlNode = resp.SelectSingleNode("//r s:stack", mgr)
Dim txt As String = sk.InnerText
Dim bs As XmlNodeList = sk.SelectNodes("//rs:bills ", mgr)
Dim b As XmlNode
For Each b In bs
Dim billid As String
billid = b.SelectSingleNode("//rs:b ill/rs:bil lid", mgr).InnerText
Next
MY QUESTION IS HOW COME THE bs.Count = 1 when there are more than one?
Please help
Dim doc As New Xml.XmlDocument
doc.Load("c:/temp/R1ExCR20
Dim mgr As XmlNamespaceManager = New XmlNamespaceManager(doc.Na
mgr.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/")
mgr.AddNamespace("rs", "https://secure.abccompany.com/core/")
' Dim nodelist As XmlNodeList = doc.SelectNodes("/soap:Env
Dim envelope As XmlNode = doc.DocumentElement
Dim body As XmlNode = envelope.SelectSingleNode(
Dim resp As XmlNode = body.SelectSingleNode("//r
Dim sk As XmlNode = resp.SelectSingleNode("//r
Dim txt As String = sk.InnerText
Dim bs As XmlNodeList = sk.SelectNodes("//rs:bills
Dim b As XmlNode
For Each b In bs
Dim billid As String
billid = b.SelectSingleNode("//rs:b
Next
MY QUESTION IS HOW COME THE bs.Count = 1 when there are more than one?
Please help
Dim bs As XmlNodeList = sk.SelectNodes("//rs:bills ", mgr)
well, there is only one rs:bills element in your XML
the rs:bills has two rs:bill children
but that is not what you are iterating over.
You are iterating over the one rs:bills
well, there is only one rs:bills element in your XML
the rs:bills has two rs:bill children
but that is not what you are iterating over.
You are iterating over the one rs:bills
in your XML you have soap:Body
XML element names are case sensitive