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