XML in VB having issue with child nodes included in for loop

43 Views Asked by At

I am trying to pull xml data out in a specific order. I need to know why in the section marked with the ***** that this loop is pulling data from the descendant nodes. i only need nodes from that node 'level'. If anyone can help thanks allot.

The post is making me add more text but i have nothing more to say about it.

 Public Sub importPies(piesxml As String, outputcsv As String, version1 As String, pwat As frmDetailProgress)


        Dim xmlDoc As XElement
        Dim xmlNodes, currentNode As IEnumerable(Of XElement)
        Dim textout As String = ""
        Dim x, xl As Long
        Dim RootElements() As String
        Dim nsname As String
        Dim fileroot As String
        Dim primaryElement(0) As String


        Dim fileStreams() As PiesFileStream
        Dim dataline(0) As PIESDataLine
        fileroot = Path.GetFileNameWithoutExtension(outputcsv)
        xmlDoc = XElement.Load(piesxml)
        nsname = "{" & xmlDoc.Elements().ElementAt(1).Name.Namespace.ToString & "}"
        pwat.Invoke(Sub() pwat.add("Setting up for PIES " & version1 & " Conversion to csv"))
        fileStreams = CreatePIESCsvHeader(outputcsv, version1, False)

        ' get root elements

        RootElements = GetPiesRootElements(version1)
        For x = 1 To RootElements.Length - 1
            xmlNodes = From nm In xmlDoc.Elements(nsname & RootElements(x))
            ReDim Preserve primaryElement(primaryElement.Length)
            For xl = 0 To xmlNodes.Count - 1

                If xmlNodes.Elements(xmlNodes.Elements.ElementAt(xl).Name.ToString).Count > 1 Then
                    primaryElement(primaryElement.Length - 1) = xmlNodes.Elements.ElementAt(xl).Name.LocalName.ToString
                    Exit For
                End If

            Next xl



        Next x
        pwat.Invoke(Sub() pwat.add("Starting Conversion to csv .", "G"))
        For x = 1 To RootElements.Length - 1
            pwat.Invoke(Sub() pwat.add("Decoding " & RootElements(x)))

            currentNode = From nm In xmlDoc.Elements(nsname & RootElements(x)) Where nm.Name.LocalName = RootElements(x)

            ' check to see if any nodes exist based from current pies def
            If currentNode.Count > 0 Then

                ' check to see if first node has multiples, if so we will dig into it otherwise we will build from here

                If currentNode(0).DescendantNodes.Count = 1 Then

                    For Each node In currentNode
                        ReDim Preserve dataline(dataline.Length)
                        dataline(dataline.Length - 1) = New PIESDataLine
                        dataline(dataline.Length - 1).FieldName = node.Name.LocalName
                        dataline(dataline.Length - 1).Data = node.Value
                    Next
                    WritePiesData(dataline, fileStreams, RootElements(x))
                    pwat.Invoke(Sub() pwat.add("Single record Decode Method", "G"))
                Else
                    pwat.Invoke(Sub() pwat.add("Multiple record Decode method data lines =" & currentNode.LongCount.ToString, "R"))

                    ' ***** problem is here descendent nodes are included 

                    For Each node As XElement In currentNode

                        ReDim dataline(0)

                        decodeNode(node, dataline)
                        WritePiesData(dataline, fileStreams, RootElements(x))

                    Next
                End If

            Else

                pwat.Invoke(Sub() pwat.add("** No Data **", "R"))
            End If

        Next

currentNode.descendantnodes

        CloseFileStreams(fileStreams)
        pwat.Invoke(Sub() pwat.add("Done !"))
        pwat.Invoke(Sub() pwat.EndAndClose())







    End Sub
0

There are 0 best solutions below