Solved

VB.NET XML Processing a XMLNodeList with Namespace

Posted on 2016-08-16
16
61 Views
Last Modified: 2016-08-17
OK based on the below code we Load the Doc and get the 3 Call Nodes. My question now is how can i do the folowing things

a) Get a list of all sub nodes of this node
If posible so i could loop thru that list
b) Check if a specific node exists
<NPInfo>
c) Access data in a subnode for example how would i access the
<OrigParty>
<SubscriberAddr type="e164">+12095551122</SubscriberAddr>
</OrigParty>

Imports System.Xml
Imports System.Xml.XPath

Dim m_xmld As XmlDocument
Dim m_nodelist As XmlNodeList
Dim m_node As XmlElement

'Create the XML Document
m_xmld = New XmlDocument()
'Load the Xml file
m_xmld.Load("sample2.xml")

'' Added these two lines
Dim nsmgr = New XmlNamespaceManager(m_xmld.NameTable)
nsmgr.AddNamespace("ns", "http://www.metaswitch.com/cfs/billing/V1.0")

'Get the list of name nodes
'' Modified your XPath and added the second parameter 
m_nodelist = m_xmld.SelectNodes("/ns:File/ns:CDRs/ns:Call", nsmgr)
'Loop through the nodes

Open in new window

0
Comment
Question by:AlexPonnath
  • 8
  • 8
16 Comments
 
LVL 35

Accepted Solution

by:
YZlat earned 500 total points
ID: 41758121
to get all children nodes use recursive. The code below will retrieve InnerText of all the nodes:

Sub GetAllChildNodes(ByVal parentNode As XmlNode)
        For Each childNode As XmlNode In parentNode.ChildNodes
            Console.Write(childNode.InnerText)
            GetAllChildNodes(childNode)
        Next
    End Sub

Open in new window


to check for particular node, you can use XPath

m_node= m_xmld.SelectSingleNode("//OrigParty/SubscriberAddr[@type]")

Open in new window


as well as to retrieve particular node:

m_nodelist= m_xmld.SelectNodes("//OrigParty/SubscriberAddr").InnerText

Open in new window

1
 

Author Comment

by:AlexPonnath
ID: 41758252
Ok, that answers 1 and 2 somewhat, but how about 3. Lets say i have the Node selected via
Dim thisNode As XmlNode = m_nodelist(temp).SelectSingleNode("ns:RoutingInfo", nsmgr)

Open in new window

The node looks like this
<RoutingInfo>
<RequestedAddr type="unknown">17145551212</RequestedAddr>
<DestAddr type="e164">+17145551212</DestAddr>
<RoutedAddr type="national">7145551212</RoutedAddr>
<CallingPartyRoutedAddr type="national">7145551212</CallingPartyRoutedAddr>
<CallingPartyOrigAddr type="national">7145551212</CallingPartyOrigAddr>
</RoutingInfo>

Open in new window

How can i get the Attribute (Type) and Value for <RoutedAddr>
0
 
LVL 35

Expert Comment

by:YZlat
ID: 41758257
type=thisNode.SelectSingleNode("//RouteAddr[@type]")
val=thisNode.SelectSingleNode("//RouteAddr").InnerText

Open in new window

0
 

Author Comment

by:AlexPonnath
ID: 41758496
This code seems to not work for doc with name space... it returns nothing
0
 
LVL 35

Expert Comment

by:YZlat
ID: 41758535
What if you add namespaces?

type=thisNode.SelectSingleNode("//ns:RouteAddr[@type]",, nsmgr)
val=thisNode.SelectSingleNode("//ns:RouteAddr", , nsmgr).InnerText

Open in new window

0
 

Author Comment

by:AlexPonnath
ID: 41758634
Still doesn't work, here is the code i used to test this code.. I also included sample data


    Dim m_xmld As XmlDocument
        Dim m_nodelist As XmlNodeList
        Dim m_node As XmlElement
        Try




            'Create the XML Document
            m_xmld = New XmlDocument()
            'Load the Xml file
            m_xmld.Load("C:\Documents\sample2.xml")

            '' Added these two lines
            Dim nsmgr = New XmlNamespaceManager(m_xmld.NameTable)
            nsmgr.AddNamespace("ns", "http://www.metaswitch.com/cfs/billing/V1.0")

            'Get the list of name nodes
            '' Modified your XPath and added the second parameter 
            m_nodelist = m_xmld.SelectNodes("/ns:File/ns:CDRs/ns:Call", nsmgr)
            'Loop through the nodes

            For temp As Integer = 0 To m_nodelist.Count - 1

                MsgBox(m_nodelist(temp).Attributes.GetNamedItem("seqnum").Value)

                Dim thisNode As XmlNode = m_nodelist(temp).SelectSingleNode("ns:RoutingInfo", nsmgr)


                If thisNode Is Nothing Then

                Else

                    MsgBox(thisNode.OuterXml)
                    MsgBox(thisNode.SelectSingleNode("//ns:RouteAddr[@type]", nsmgr))
                    MsgBox(thisNode.SelectSingleNode("//ns:RouteAddr", nsmgr).InnerText)

                End If

                For Each childnode As XmlNode In m_nodelist(temp)
                    MsgBox(childnode.Name)
                Next

            Next


            MsgBox(m_nodelist.Count)
        Catch ex As Exception
            Throw New Exception("Failed to load xml content. " & ex.Message)
        End Try

Open in new window

sample2.xml
0
 
LVL 35

Expert Comment

by:YZlat
ID: 41759686
What error are you getting and in what line?
0
 

Author Comment

by:AlexPonnath
ID: 41759798
If you run the code, there is no error . The problem is it does not return any of the values of those nodes. So we never get any data for the routeaddr
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 35

Expert Comment

by:YZlat
ID: 41759866
I know what the issue is. Replace the following lines:

nsmgr.AddNamespace("ns", "http://www.metaswitch.com/cfs/billing/V1.0")

            'Get the list of name nodes
            '' Modified your XPath and added the second parameter 
            m_nodelist = m_xmld.SelectNodes("/ns:File/ns:CDRs/ns:Call", nsmgr)

Open in new window


with

nsmgr.AddNamespace("xsi", "http://www.metaswitch.com/cfs/billing/V1.0")

            'Get the list of name nodes
            '' Modified your XPath and added the second parameter 
            m_nodelist = m_xmld.SelectNodes("//xsi:Call", nsmgr)

Open in new window


You just used incorrect namespace
0
 

Author Comment

by:AlexPonnath
ID: 41759915
that has no impact at all still returns the empty value. in both case the innerXML and OuterXML for the node is the same before changing your code and after

"<RequestedAddr type=""unknown"" xmlns=""http://www.metaswitch.com/cfs/billing/V1.0"">12095551212</RequestedAddr><DestAddr type=""e164"" xmlns=""http://www.metaswitch.com/cfs/billing/V1.0"">+12095551212</DestAddr><RoutedAddr type=""national"" xmlns=""http://www.metaswitch.com/cfs/billing/V1.0"">2095551212</RoutedAddr><CallingPartyRoutedAddr type=""national"" xmlns=""http://www.metaswitch.com/cfs/billing/V1.0"">2095551212</CallingPartyRoutedAddr><CallingPartyOrigAddr type=""national"" xmlns=""http://www.metaswitch.com/cfs/billing/V1.0"">2095551212</CallingPartyOrigAddr>"

           OuterXml      
<RoutingInfo xmlns=""http://www.metaswitch.com/cfs/billing/V1.0""><RequestedAddr type=""unknown"">12095551212</RequestedAddr><DestAddr type=""e164"">+12095551212</DestAddr><RoutedAddr type=""national"">2095551212</RoutedAddr><CallingPartyRoutedAddr type=""national"">2095551212</CallingPartyRoutedAddr><CallingPartyOrigAddr type=""national"">2095551212</CallingPartyOrigAddr></RoutingInfo>"      String


call.png
0
 
LVL 35

Expert Comment

by:YZlat
ID: 41760013
I tire dit on a sample file you have sent me. This looks very different
0
 

Author Comment

by:AlexPonnath
ID: 41760030
Ok, i got a bit further and found one problem which was a typo on my side i had one of the node names wrong / missing the "d" in RoutedAddr. After fixing that and resetting my code to the original i get now the inner Text of   "thisNode.SelectSingleNode("ns:RoutedAddr", nsmgr).InnerText" but the "thisNode.SelectSingleNode("ns:RoutedAddr[@type]", nsmgr).Value' still returns a blank value even so i have a value in the XML file
0
 
LVL 35

Expert Comment

by:YZlat
ID: 41760048
change it to:

thisNode.SelectSingleNode("//ns:RoutedAddr[@type]", nsmgr).Value

Open in new window

0
 

Author Comment

by:AlexPonnath
ID: 41760057
Still nothing, both return blank
0
 
LVL 35

Assisted Solution

by:YZlat
YZlat earned 500 total points
ID: 41760064
thisNode.SelectSingleNode("//ns:RoutedAddr", nsmgr).Attributes("type").Value

Open in new window

0
 

Author Comment

by:AlexPonnath
ID: 41760076
Wow, finaly we figured out this beast..
It returns now the correct value for Type
Thanks
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now