Deserialization XML

1

I have a method that does a query to a WebService, with the return I do a deserialization of the XML received. It turns out that if I do the deserialization in memory it gives an error saying:

  

xmlns="http://www.portalfiscal.inf.br/nfe" not expected

But if I do the same procedure saving the file to disk and using StreamReader it works normally. I've commented on the code which part is giving the error and which one is working. You have POG inside foreach , please ignore, unless it is the problem.

What is the reason for this error?

XML:

<resNFe xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" versao="1.00" xmlns="http://www.portalfiscal.inf.br/nfe">
    <chNFe>5616516516516516816165165156</chNFe>
    <CNPJ>12345678654231</CNPJ>
    <xNome>bla bla bla</xNome>
    <IE>651651654</IE>
    <dhEmi>2016-06-28T09:00:04-03:00</dhEmi>
    <tpNF>1</tpNF>
    <vNF>10049.69</vNF>
    <digVal>asdasdasdzc5asr41fa564s</digVal>
    <dhRecbto>2016-06-28T09:16:34-03:00</dhRecbto>
    <nProt>3531351313235151</nProt>
    <cSitNFe>1</cSitNFe>
</resNFe>

Code:

public void ConsultaDfe(string CNPJ)
{
    DistribuicaoDFe xmlDfe = new DistribuicaoDFe();

    xmlDfe.ModeloDocumento = "NFE";
    xmlDfe.Versao = "1.0";
    xmlDfe.CNPJEmissor = CNPJ;
    xmlDfe.TpAmb = "1";
    xmlDfe.UsarUltimoNSU = "N";
    ClienteWebService cliente = new ClienteWebService();
    XDocument Xml = cliente.Envio(SerializaObjeto.SerializeToString(xmlDfe), CNPJ);

    XmlSerializer xmlSerializer = new XmlSerializer(typeof(RetornoDFe));
    XmlReader reader = Xml.CreateReader();
    RetornoDFe retornoDFe = (RetornoDFe)xmlSerializer.Deserialize(reader);

    foreach (var item in retornoDFe.Documentos)
    {
        XmlNode[] _docXml = (XmlNode[])item.DocXML;
        string base64 = _docXml[0].Value.ToString();
        byte[] buffer = Convert.FromBase64String(base64);
        MemoryStream ms = new MemoryStream(buffer);
        XmlDocument doc = new XmlDocument();
        doc.Load(ms);

        XmlSerializer _xmlSerializer = new XmlSerializer(typeof(resNFe));

        XmlReader _reader = new XmlNodeReader(doc);

        //Não funciona  - xmlns:"" não esperado

        var _resNFe = (resNFe)xmlSerializer.Deserialize(_reader);

    }

    // Funciona 100%
    var resnfe = new resNFe();
    string caminho = @"C:\home160604301024000131550010001971591190360567.xml";
    XmlSerializer xmlSerializer = new XmlSerializer(typeof(resNFe));
    StreamReader reader = new StreamReader(caminho);
    resnfe = (resNFe)xmlSerializer.Deserialize(reader);
    reader.Close();
}
    
asked by anonymous 18.08.2016 / 22:54

1 answer

5

Try to pass the string instead of the XmlReader and create a stringReader for example:

public static T LoadFromXMLString<T>(string xmlText)
{
    var stringReader = new System.IO.StringReader(xmlText);
    var serializer = new XmlSerializer(typeof(T));
    return (T)serializer.Deserialize(stringReader);
}

and call

LoadFromXmlString<resNFe>(suaString);
    
19.08.2016 / 13:56