0

el sistema al que le estoy tratando de dar mantenimiento, es sobre lectura de xml. con los cambios del SAT, este aun no esta preparado para 4.0.

Actualmente esta leyendo de la siguiente manera:

public readonly XNamespace xmlns_cfdi = "http://www.sat.gob.mx/cfd/3";
public readonly XNamespace xmlns_tfd = "http://www.sat.gob.mx/TimbreFiscalDigital";
public  string version =  "3.3", NoDenominacion = "Sin Denominación" ;

En el emisor se lee asi

public string Emisor
        {
            get
            {
                XElement emisor = Root.Element(xmlns_cfdi + "Emisor");
                XAttribute atributo = emisor.Attribute("Nombre");

                return atributo != null ? atributo.Value : NoDenominacion;
            }
        }

El timbre fiscal se lee asi

 public XElement TimbreFiscalDigital
        {
            get
            {
                return Root.Element(xmlns_cfdi + "Complemento").Element(xmlns_tfd + "TimbreFiscalDigital");
            }
        }

y para validarlo es de la siguiente manera:

public bool Valido
        {
            get
            {
                if (!Root.Name.Namespace.Equals(xmlns_cfdi))
                    return false;

                if (!Root.Attribute("Version").Value.Equals(version))
                    return false;

                return true;
            }
        }

Y el problema es que no se como poder leer también o hacer condicionales si agrego lo siguiente para la validación que seria :

public readonly XNamespace xmlns_cfdi1 = "http://www.sat.gob.mx/cfd/4";
public  string version1 =  "4.0"

1 respuesta 1

0

Hola yo Hago esto pero con XmlDocument y funciona independientemente si es la version 3 o 4

 XmlDocument doc = new XmlDocument();
    doc.LoadXml(xml);

    var rootElement = doc.GetElementsByTagName("cfdi:Comprobante")[0];

    //Obtener los namespaces
    var nmCFDI = rootElement.Attributes["xmlns:cfdi"]?.Value;
    var nmtfd = rootElement.Attributes["xmlns:tfd"]?.Value;

    if (nmtfd == null)
    {
        nmtfd = doc.GetElementsByTagName("tfd:TimbreFiscalDigital")[0].Attributes["xmlns:tfd"]?.Value;
    }

    var nsmgr = new XmlNamespaceManager(doc.NameTable);
    nsmgr.AddNamespace("cfdi", nmCFDI);

    if (!string.IsNullOrEmpty(nmtfd))
    {
        nsmgr.AddNamespace("tfd", nmtfd);
    }
    

    XmlNode comprobante = doc.SelectSingleNode(xpath: "cfdi:Comprobante", nsmgr);
    XmlNode receptor = doc.SelectSingleNode(xpath: "*/cfdi:Receptor", nsmgr);
    XmlNode emisor = doc.SelectSingleNode(xpath: "*/cfdi:Emisor", nsmgr);
    XmlNode timbre = doc.SelectSingleNode(xpath: "*/cfdi:Complemento/tfd:TimbreFiscalDigital", nsmgr);

O tsambie podria ser asi

  class Documento {
        public readonly string namespace_cfdi3 = "http://www.sat.gob.mx/cfd/3";
        public readonly string namespace_tfd = "http://www.sat.gob.mx/TimbreFiscalDigital";
        public readonly string namespace_cfdi4 = "http://www.sat.gob.mx/cfd/4";
        public string version = "";
        public string NoDenominacion = "Sin Denominación";

        public readonly XNamespace xmlns_cfdi;
        public readonly XNamespace xmlns_tfd;

        public XElement Root;

        public Documento(string xml) {

            //Aqui cargas el root

            //Saber que version es
            version = xml.Contains(namespace_cfdi3) ? "3.3" : "4.0";
            xmlns_cfdi = version == "3.3" ? namespace_cfdi3 : namespace_cfdi4;
            xmlns_tfd = namespace_tfd;
        }

        public string Emisor
        {
            get
            {
                XElement emisor = Root.Element(xmlns_cfdi + "Emisor");
                XAttribute atributo = emisor.Attribute("Nombre");

                return atributo != null ? atributo.Value : NoDenominacion;
            }
        }

    }
1

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.