XML deserialization

Asked

Viewed 933 times

1

I have a method that makes a query to a Webservice, with the return I do a deserialization of the received XML. It turns out that if I do the desirialization in memory it gives a mistake saying:

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

But if I do the same procedure saving the file on disk and using the StreamReader it works normally. I commented in the code which part is giving the error and which one is working.
There’s POG inside the foreach, please ignore, unless she’s the problem.

Why this error occurred?

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:\home\13160604301024000131550010001971591190360567.xml";
    XmlSerializer xmlSerializer = new XmlSerializer(typeof(resNFe));
    StreamReader reader = new StreamReader(caminho);
    resnfe = (resNFe)xmlSerializer.Deserialize(reader);
    reader.Close();
}

1 answer

5


Try passing the string instead of Xmlreader and creating 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);
  • 1

    Young, it worked wonderfully. I missed a day with it. Thank you for sharing your knowledge. I’ve already searched and I still don’t understand why you made a mistake with Xmlreader.......

Browser other questions tagged

You are not signed in. Login or sign up in order to post.