Link to home
Create AccountLog in
Avatar of LilyRock
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.NameTable)
        mgr.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/")
        mgr.AddNamespace("rs", "https://secure.abccompany.com/core/")
        Dim nodelist As XmlNodeList = doc.SelectNodes("soap:Envelope/soap:body/rs:RepriceStack/rs:stack/", mgr)
        Dim node As XmlNode
        For Each node In nodelist
            Dim strg As String
            strg = node.SelectSingleNode("rs:bills").InnerText
        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><RepriceStack xmlns="https://secure.abccompany.com/core">
<stack><testmode>true</testmode><clientid>abccompany</clientid>
<bills>
<bill><billid>123</bilid><desc>Post it</desc></bill>
<bill><billid>1234</billid><desc>Note pad</desc></bill>
</bills>
</stack></RepriceStack></soap:Body></soap:Envelope>
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

in youur XPath you have soap:body
in your XML you have soap:Body

XML element names are case sensitive
Avatar of LilyRock
LilyRock

ASKER

Thanks for your prompt answer.
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
I changed to this

Dim nodelist As XmlNodeList = doc.SelectNodes("soap:Envelope/soap:body/rs:RepriceStack/rs:stack", mgr)

It didn't give any error but why the nodelist.Count = 0?

Sorry for the typos. I misplaced my eye glass.
ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
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?
I changed my codes around and get to this point

Dim doc As New Xml.XmlDocument
        doc.Load("c:/temp/R1ExCR20110331124318.xml")
        Dim mgr As XmlNamespaceManager = New XmlNamespaceManager(doc.NameTable)
        mgr.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/")
        mgr.AddNamespace("rs", "https://secure.abccompany.com/core/")
        ' Dim nodelist As XmlNodeList = doc.SelectNodes("/soap:Envelope/soap:Body/rs:RepriceStack/rs:stack/rs:bills", mgr)
        Dim envelope As XmlNode = doc.DocumentElement
        Dim body As XmlNode = envelope.SelectSingleNode("//soap:Body", mgr)
        Dim resp As XmlNode = body.SelectSingleNode("//rs:RepriceStack", mgr)
        Dim sk As XmlNode = resp.SelectSingleNode("//rs: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:bill/rs:billid", mgr).InnerText
 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