Save CFDI payment elements in c #

3

I have an XML CFDI version 3.3 and I want to save the attributes of the DoctoRelated node. It works for me to save the attributes of the first node, the problem is that in some XML I get more than 1 node DoctoRelacionado and only saves the first one. How do I save the n nodes that come?

As you can see, in what I have said, I tried to do the following:

  • Save the attributes of the first DoctoRelated node. FACT.
  • Ask if there is a second node. FACT.
  • Delete the first DoctoRelated node so that my second node becomes my first node and so do it n times.
  • But it did not work, I think I have to do a foreach or something, but I do not really know how. I appreciate your help.

    Here my method:

    private bool cargar_Complementos(ref Factura factura, string xml)
        {       
            try
            {
                XNamespace cfdi = "http://www.sat.gob.mx/cfd/3";
                XNamespace tfd = "http://www.sat.gob.mx/TimbreFiscalDigital";
                XNamespace bfa2 = "http://www.buzonfiscal.com/ns/addenda/bf/2";
                XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
                XNamespace pago10 = "http://www.sat.gob.mx/Pagos";
    
                XDocument xdoc = XDocument.Parse(xml);
    
                Comprobante = xdoc.Element(cfdi.GetName("Comprobante"));
    
                Fecha = Convert.ToString(Comprobante.Attribute("Fecha").Value);
                Folio = Convert.ToString(Comprobante.Attribute("Folio").Value);
                Serie = Convert.ToString(Comprobante.Attribute("Serie").Value);
    
                Complemento = Comprobante.Element(cfdi.GetName("Complemento"));
    
                Pagos = Complemento.Element(pago10.GetName("Pagos"));
    
                Pago = Pagos.Element(pago10.GetName("Pago"));
                Pago = Pagos.Element(pago10.GetName("Pago"));
    
                DoctoRelacionado = Pago.Element(pago10.GetName("DoctoRelacionado"));
    
                //if (contador != 0)
                //{
                //    int aux = contador;
                //    while (aux != 0)
                //    {
                //        DoctoRelacionado.Remove();
                //        aux--;
                //    }
    
                //}
    
                ImpPagado = Convert.ToDecimal((DoctoRelacionado.Attribute("ImpPagado").Value));
                IdDocumento = Convert.ToString((DoctoRelacionado.Attribute("IdDocumento").Value));
                TimbreFiscalDigital = Complemento.Element(tfd.GetName("TimbreFiscalDigital"));
                UUID = Convert.ToString((TimbreFiscalDigital.Attribute("UUID").Value));
    
                //contador++;
    
                return true;
            }
            catch
            {
                return false;
            }
        }
    

    Here my XML:

    <?xml version="1.0" encoding="UTF-8"?>
    <cfdi:Comprobante xmlns:xsi="" xmlns:cfdi="http://www.sat.gob.mx/cfd/3" Version="3.3" Serie="VRI" Folio="21511" Fecha="2017-12-14T11:40:01" NoCertificado="" SubTotal="0" Moneda="XXX" Total="0" TipoDeComprobante="P" LugarExpedicion="" xsi:schemaLocation="http://www.sat.gob.mx/cfd/3 http://www.sat.gob.mx/sitio_internet/cfd/3/cfdv33.xsd" Sello="/+Oi472PgRD/tVkMw==" Certificado="+/=">
      <cfdi:Emisor Rfc="" Nombre=""/>
      <cfdi:Receptor Rfc="" Nombre="" UsoCFDI=""/>
      <cfdi:Conceptos>
        <cfdi:Concepto ClaveProdServ="" Cantidad="1" ClaveUnidad="ACT" Descripcion="Pago" ValorUnitario="0" Importe="0"/>
      </cfdi:Conceptos>
      <cfdi:Complemento>
        <pago10:Pagos xmlns:pago10="http://www.sat.gob.mx/Pagos" Version="1.0">
          <pago10:Pago FechaPago="2017-12-14T11:44:47" FormaDePagoP="03" MonedaP="MXN" Monto="7951.13">
            <pago10:DoctoRelacionado IdDocumento="3AC5077C-41D1-8C4B-" Serie="VSRI" Folio="86707" MonedaDR="MXN" MetodoDePagoDR="PPD" NumParcialidad="1" ImpSaldoAnt="5044.04" ImpPagado="5044.04" ImpSaldoInsoluto="0.00"/>
            <pago10:DoctoRelacionado IdDocumento="-2D1E-4B58-825D-" Serie="VSRI" Folio="86708" MonedaDR="MXN" MetodoDePagoDR="PPD" NumParcialidad="1" ImpSaldoAnt="2907.09" ImpPagado="2907.09" ImpSaldoInsoluto="0.00"/>
          </pago10:Pago>
        </pago10:Pagos><tfd:TimbreFiscalDigital Version="1.1" UUID="-B806-4BE5-A0D8-9E8202A6BEB4" FechaTimbrado=":44:58" RfcProvCertif="FEL100622S88" SelloCFD="DDAF70qWqmYPFBfcP8R1Ph8EzMQTV8o1XCEmfQ/uh+PVb/xVj14HVnVwMxWnP0yyPU/D/tVkMw==" NoCertificadoSAT="" xmlns:tfd="http://www.sat.gob.mx/TimbreFiscalDigital"  SelloSAT="/J8A==" xsi:schemaLocation="http://www.sat.gob.mx/TimbreFiscalDigital http://www.sat.gob.mx/sitio_internet/cfd/TimbreFiscalDigital/TimbreFiscalDigitalv11.xsd"  />
      </cfdi:Complemento>
    </cfdi:Comprobante>
    
        
    asked by Gerry Targaryen 23.02.2018 в 19:39
    source

    2 answers

    4

    Yes, exactly.

    When doing element, you are only getting the first one you find.

    You are actually looking for the Elements method, which returns a collection of the children of that element.

    Therefore, you could do something like

    foreach(var v in Pago.Elements(pago10.GetName("DoctoRelacionado")))
    {
        //tu codigo aca
    }
    
        
    answered by 23.02.2018 / 19:45
    source
    -1

    Try this:

    private void button1_Click(object sender, EventArgs e)
        {
            DataSet dataSet = new DataSet();
            dataSet.ReadXml(@"C:\Users\Usuario\Downloads\EJ_CFDI_DIC_2018.xml");
           // dataSet.ReadXml(@"C:\Users\Usuario\AppData\Local\TempzEC66F9EB2\Books.xml");
            dataGridView1.DataSource = dataSet.Tables[0];
            dataGridView2.DataSource = dataSet.Tables[1];
            dataGridView3.DataSource = dataSet.Tables[2];
            dataGridView4.DataSource = dataSet.Tables[4];
            dataGridView5.DataSource = dataSet.Tables[5];
            dataGridView6.DataSource = dataSet.Tables[6];
            dataGridView7.DataSource = dataSet.Tables[7];
            dataGridView8.DataSource = dataSet.Tables[8];
            dataGridView9.DataSource = dataSet.Tables[9];
            dataGridView10.DataSource = dataSet.Tables[3][1];
            //dataGridView11.DataSource = dataSet.Tables[11];
         }
    
        
    answered by 18.01.2019 в 18:18