0

Estoy realizando un programa en donde extraigo datos de un archivo XML y los almaceno en un listView, el problema radica en que si encuentra más de 1 atributo tengo que repetir el valor del elemento en la siguiente fila o dejarlo en blanco.

Anexo mi código, en la que funciona a la perfección si solo encuentra un atributo en el elemento.

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

El XML está estructurado de la siguiente manera

<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>

Anexo una captura para ver si me pueden orientar introducir la descripción de la imagen aquí

De antemano muchas gracias.

1 respuesta 1

0

Con este código logré resolver mi problema, por si alguien le sirve.

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

La imagen de como quedó: introducir la descripción de la imagen aquí

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.