Extract CFDI XML Data in VB.Net

0

I'm doing a program where I extract data from an XML file and store it in a listView, the problem is that if it finds more than 1 attribute I have to repeat the value of the element in the next row or leave it blank.

Annex my code, in which it works perfectly if you only find an attribute in the element.

Try
        Dim m_xmld As XmlDocument
        Dim m_nodelist As XmlNodeList
        Dim m_nodelist1 As XmlNodeList
        Dim m_node As XmlNode

        'Creamos el "XML Document"
        m_xmld = New XmlDocument()

        'Cargamos el archivo
        m_xmld.Load(Trim(txtDirectorio.Text))

        'Obtenemos la lista de los nodos "name"
        m_nodelist = m_xmld.GetElementsByTagName("cfdi:InformacionAduanera")
        m_nodelist1 = m_xmld.GetElementsByTagName("cfdi:Concepto")

        'Recorrer XML sacar más de un NoIdentificacion
        For Each m_node In m_nodelist1
            lvNoIdentificacion.Items.Add(m_node.Attributes.GetNamedItem("NoIdentificacion").Value)
        Next

        'Recorrer XML sacar más de un NumeroPedimento
        For Each m_node In m_nodelist
            lvNumeroPedimento.Items.Add(m_node.Attributes.GetNamedItem("NumeroPedimento").Value)
        Next

        'Recorrer los item de la lista 
        'Verificar que contenga 15 digitos el NumeroPedimento (Length=21 por los espacios en blanco entre grupos de digitos)
        'Si el NumeroPedimento no contiene cadena de 15 digitos entonces pintar color Rojo.
        Dim i As Integer
        For i = 0 To lvNumeroPedimento.Items.Count - 1
            If lvNumeroPedimento.Items(i).Text.Length <> 21 Then
                lvNumeroPedimento.Items.Item(i).ForeColor = System.Drawing.Color.Red
            Else
                lvNumeroPedimento.Items.Item(i).ForeColor = System.Drawing.Color.Black
            End If
        Next

    Catch ex As Exception
        MsgBox(ex.Message)

    End Try

The XML is structured as follows

<cfdi:Concepto ClaveProdServ="43223300" NoIdentificacion="AX101100" Cantidad="4" ClaveUnidad="KT" Unidad="kit" Descripcion="Kit Separador de Fibra en campo P/6 Fibras" ValorUnitario="612.00" Importe="2448.00" Descuento="678.34">
        <cfdi:Impuestos>
            <cfdi:Traslados>
                <cfdi:Traslado Base="1769.66" Impuesto="002" TipoFactor="Tasa" TasaOCuota="0.160000" Importe="283.15" />
            </cfdi:Traslados>
        </cfdi:Impuestos>
        <cfdi:InformacionAduanera NumeroPedimento="XX  XX  38XX  4XX1254" />
        <cfdi:InformacionAduanera NumeroPedimento="XX  XX  38XX  400XX41" />
    </cfdi:Concepto>

Attach a screenshot to see if you can guide me

Thank you in advance.

    
asked by Silvestre Silva 15.12.2017 в 23:08
source

1 answer

0

With this code I managed to solve my problem, in case someone serves you.

Try
        Dim doc As XmlDocument
        Dim concepto As XmlNodeList
        Dim Elemento As XmlNode
        Dim subnodo As XmlElement

        ' Creamos el "XML Document"
        doc = New XmlDocument()

        'Cargamos el archivo
        doc.Load(Trim(txtDirectorio.Text))

        'Obtenemos la lista de los nodos "cfdi:Concepto"
        concepto = doc.GetElementsByTagName("cfdi:Concepto")

        'Recorrer XML sacar más de un NoIdentificacion
        For Each Elemento In concepto
            Dim cadena As String
            For Each subnodo In Elemento

                cadena = Trim(subnodo.GetAttribute("NumeroPedimento"))
                If cadena.Length = 0 Then
                Else
                    lvNumeroPedimento.Items.Add(cadena)
                    lvNoIdentificacion.Items.Add(Elemento.Attributes.GetNamedItem("NoIdentificacion").Value)
                    lvCantidad.Items.Add(Elemento.Attributes.GetNamedItem("Cantidad").Value)
                End If
            Next

            If cadena.Length = 0 Then
            Else
                lvNoIdentificacion.Items.Add("------------------------------")
                lvCantidad.Items.Add("----------")
                lvNumeroPedimento.Items.Add("-------------------------------")
            End If
        Next

        'Recorrer los item de la lista 
        'Verificar que contenga 15 digitos el NumeroPedimento (Length=21 por los espacios en blanco entre grupos de digitos)
        'Si el NumeroPedimento no contiene cadena de 15 digitos entonces pintar color Rojo.
        Dim i As Integer
        For i = 0 To lvNumeroPedimento.Items.Count - 1
            If lvNumeroPedimento.Items(i).Text.Length <> 21 Then
                lvNumeroPedimento.Items.Item(i).ForeColor = System.Drawing.Color.Red
                lvCantidad.Items.Item(i).ForeColor = System.Drawing.Color.Red
                lvNoIdentificacion.Items.Item(i).ForeColor = System.Drawing.Color.Red
            Else
                lvNumeroPedimento.Items.Item(i).ForeColor = System.Drawing.Color.Black
            End If

            If lvNumeroPedimento.Items(i).Text = "-------------------------------" Then
                lvNumeroPedimento.Items.Item(i).ForeColor = System.Drawing.Color.Black
                lvNoIdentificacion.Items.Item(i).ForeColor = System.Drawing.Color.Black
                lvCantidad.Items.Item(i).ForeColor = System.Drawing.Color.Black
            End If
        Next

    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

The image of how it was:

    
answered by 22.12.2017 / 19:27
source