Do not differentiate CFDI attribute names in XML

3

I'm doing an application that is responsible for separating XML files based on some parameters, that's fine, but now with the changes of the CFDI, my programming thundered, since users can have mixed the documents of cfdi 3.2 and 3.3 in the same folder.

What strikes me the route of the documents is that in version 3.2 I obtained the data in this way:

strVersion = doc.GetElementsByTagName("cfdi:Comprobante")(0).Attributes("version").Value

but now with the change to 3.3 it fails me since the first letter of the attributes was changed to uppercase, and now it would be

strVersion = doc.GetElementsByTagName("cfdi:Comprobante")(0).Attributes("Version").Value

There will be a way that in case the attribute ("version") does not exist then take the value ("Version") , or in Attributes not distinguish between version and Version , I hope to understand and can suggest something to me.

Annex the structure of the XML to see if you can recover their values in a different way than what I do.

  

Version 3.3

<?xml version="1.0" encoding="UTF-8"?>
<cfdi:Comprobante Certificado="MIQ=" Fecha="2018-02-01T01:56:11" Folio="012180001054132653" FormaPago="03" LugarExpedicion="06600"  TipoCambio="1" TipoDeComprobante="I" Total="0000.00" Version="3.3" xmlns:cfdi="http://www.sat.gob.mx/cfd/3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sat.gob.mx/cfd/3 http://www.sat.gob.mx/sitio_internet/cfd/3/cfdv33.xsd">
    <cfdi:Emisor Nombre="BBVA BANCOMER, S A" RegimenFiscal="w232" Rfc="RFSGEGHSFS"/>
    <cfdi:Receptor Nombre="XXXXXXXXXXXXX" Rfc="XXXXXXXXXXXXXXXXXX" UsoCFDI="XXX"/>  
</cfdi:Comprobante>
  

Version 3.2

<?xml version="1.0" encoding="UTF-8"?>
<cfdi:Comprobante xsi:schemaLocation="http://www.sat.gob.mx/cfd/3 http://www.sat.gob.mx/sitio_internet/cfd/3/cfdv32.xsd http://www.sat.gob.mx/ine http://www.sat.gob.mx/sitio_internet/cfd/ine/ine11.xsd" version="3.2" serie="TF" sello="J="  noCertificado="00001000000402403936" certificado="MI=" condicionesDePago="PAGO INMEDIATO SIN DESC. POR P" subTotal="0.00" TipoCambio="1.00000" Moneda="MXN" total="0.00" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cfdi="http://www.sat.gob.mx/cfd/3">
    <cfdi:Emisor rfc="RFCGF242FD" nombre="Lorem">       
    </cfdi:Emisor>
    <cfdi:Receptor rfc="EWCVFFSFDF" nombre="Lorem imp">     
    </cfdi:Receptor>
</cfdi:Comprobante>
    
asked by Silvestre Silva 23.08.2018 в 23:27
source

1 answer

2

For XML in .NET I recommend you use the XDocument class and its related ones.

XDocument xDoc = XDocument.Load("32.xml");
//XDocument xDoc = XDocument.Load("33.xml");
XAttribute xAttVersion = null;
xAttVersion = xDoc.Root.Attribute("version");
if(xAttVersion == null) xAttVersion = xDoc.Root.Attribute("Version");
string version = xAttVersion.Value;
Console.WriteLine(version);

UPDATE:

Hello to get nodes is with XElement, following the previous example would be:

XNamespace ns_cfdi_3 = "http://www.sat.gob.mx/cfd/3";
XElement xReceptor = xDoc.Root.Element(ns_cfdi_3 + "Receptor");
string rfcReceptor = xReceptor.Attribute("Rfc").Value;

That is, Root is the root or first node, hence with XElement you access the following nodes using the namespace to which it belongs. The attributes of the node for this case of CFDI do not require namespace, you only indicate the name.

    
answered by 24.08.2018 / 16:47
source