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
?– rubStackOverflow
It’s a Xmldocument
– Robss70
What content after
XmlReader _reader = new XmlNodeReader(doc);
– rubStackOverflow
After this part already goes to var _resNFe = (resNFe)xmlSerializer.Deserialize(_Reader); that’s where the exception happens.
– Robss70
I was actually wondering what the content of
_reader
after executingXmlReader _reader = new XmlNodeReader(doc);
It may be that the result does not come in the expected format for deserialization.– rubStackOverflow
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.
– Robss70