VB.NET XML Processing a XMLNodeList with Namespace
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.XmlImports System.Xml.XPathDim m_xmld As XmlDocumentDim m_nodelist As XmlNodeListDim m_node As XmlElement'Create the XML Documentm_xmld = New XmlDocument()'Load the Xml filem_xmld.Load("sample2.xml")'' Added these two linesDim 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
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
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
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
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)
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)
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
I tire dit on a sample file you have sent me. This looks very different
0
AlexPonnathAuthor Commented:
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
Open in new window
to check for particular node, you can use XPath
Open in new window
as well as to retrieve particular node:
Open in new window