[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 86
  • Last Modified:

XML file fails to process correctly

I have an XML file which i am trying to process and for some reason it just doesn't work correctly

The code i use to test is

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        Try
            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("C:\Users\public\Documents\sample2.xml")

            'Get the list of name nodes 
            m_nodelist = m_xmld.SelectNodes("File/CDRs/Call")
            'Loop through the nodes


            MsgBox(m_nodelist.Count)
        Catch errorVariable As Exception
            'Error trapping
            Console.Write(errorVariable.ToString())
            Console.ReadLine()
        End Try

    End Sub

Open in new window

sample2.xml
0
AlexPonnath
Asked:
AlexPonnath
  • 2
1 Solution
 
Fernando SotoCommented:
Your XML document is using XNamespace which you are not using in your code.
0
 
AlexPonnathAuthor Commented:
What would i have to change in my code to use the XNamespace ?
0
 
Fernando SotoCommented:
Here Alex, try it this way and see if you get results.
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


MsgBox(m_nodelist.Count)

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now