Differences between Xmlreader, Streamreader and Stringreader in desiccation

Asked

Viewed 165 times

1

I asked the following question here at stackoverflow question link.

During deserialization depending on the stream used, an error may occur in the XML namespaces, depending on the question link.

Examples of code with namespace error.

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);

Code that works:

     // 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();

This one also works:

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);
}

Remembering that the same XML and the same serialized class are being used.

Can anyone tell why this namespace error depends on the stream used ?

Xmldocument:

<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>
  • What is the content of _reader ?

  • It’s a Xmldocument

  • What content after XmlReader _reader = new XmlNodeReader(doc);

  • After this part already goes to var _resNFe = (resNFe)xmlSerializer.Deserialize(_Reader); that’s where the exception happens.

  • 1

    I was actually wondering what the content of _reader after executing XmlReader _reader = new XmlNodeReader(doc); It may be that the result does not come in the expected format for deserialization.

  • 1

    Entedi, I observed that the result is apparently the same in all the examples. Even I did some tests with other Xml`s and the stream is always the same in all examples.

Show 1 more comment

1 answer

1


Seems to be a bug in the implementation of XmlNodeReader that they didn’t want to solve. This page here (which, by the way, leads to that answer here) has more details about the bug.

On the page itself, one of the contributors said that it was decided not to fix the bug. So, the best is to use StreamReader or StringReader.

EDIT: good, I think I can explain better what happens. No XmlNodeReader, the code that will compare the namespaces is similar to this one:

(object) ((System.Xml.XmlQualifiedName)xsiType).Namespace == (object)id2_namespace))

It means that it makes a comparison of objects (which is made by reference), not a comparison of strings (which is made by value). As an object is only equal to itself, this comparison always gives problem.

  • Thank you so much for the explanation, I would never have imagined that this mistake would be a bug......

Browser other questions tagged

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