0

Ok, here is my issue, i have some code which was written to downlaod an File from an FTP server and then process the XML file. All good for a long time but now the XML file has a Namespace and my code no longer works. Here is the code in question which finds all call nodes and then processes them

    Public Shared Function ParseXMLAndSave(ByVal xmlContent As String) As Generic.List(Of CallDetailRecordDataType)

    Dim xmlDoc As New XmlDocument
    Try
        xmlDoc.LoadXml(xmlContent)
    Catch ex As Exception
        Throw New Exception("Failed to load xml content. " & ex.Message)
    End Try

    Dim nodes As XmlNodeList = xmlDoc.SelectNodes("File/CDRs/Call")
    If nodes.Count = 0 Then Throw New Exception("No data node [File/CDRs/Call] found")

    Dim list As New Generic.List(Of CallDetailRecordDataType)

    For temp As Integer = 0 To nodes.Count - 1
        Try
            Dim record As CallDetailRecordDataType = ParseMainNode(nodes(temp))
            list.Add(record)
            SaveCallRecord(record)

        Catch ex As Exception
            Trace.WriteLine("Failed to parse node. " & ex.Message)
        End Try

    Next

    Return list
End Function

So what do i need to do to be able to use to find those nodes again after my xml file has now a NameSpace ?

The Namespace looks like this in the XML Doc

<File xmlns="http://www.metaswitch.com/cfs/billing/V1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" compatibility="2">
NoSoup4you
  • 640
  • 1
  • 10
  • 34

1 Answers1

0

See: How to Select XML Nodes with XML Namespaces from an XmlDocument?

For your case the code should be like this. However without your xml document I haven't been able to validate this.

    Dim xmlDoc As New XmlDocument
    Try
        xmlDoc.LoadXml(xmlContent)
    Catch ex As Exception
        Throw New Exception("Failed to load xml content. " & ex.Message)
    End Try

    'Create a namespace manager
    Dim nsmgr As New XmlNamespaceManager(xmlDoc.NameTable)
    'Add a default namespace.
    nsmgr.AddNamespace("", "http://www.metaswitch.com/cfs/billing/V1.0")

    'Pass the namespaces into the query
    Dim nodes As XmlNodeList = xmlDoc.SelectNodes("File/CDRs/Call", nsmgr)
    If nodes.Count = 0 Then Throw New Exception("No data node [File/CDRs/Call] found")
Community
  • 1
  • 1
FloatingKiwi
  • 4,408
  • 1
  • 17
  • 41