Skip to content

Instantly share code, notes, and snippets.

@lucasselliach
Forked from Pierry/gist:2d266a9b204fff3a999c
Last active September 9, 2015 14:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucasselliach/8541815c22a3ee62e8b3 to your computer and use it in GitHub Desktop.
Save lucasselliach/8541815c22a3ee62e8b3 to your computer and use it in GitHub Desktop.
XmlSerializer to NFE
public XmlDocument CriarArquivoEnvio()
{
var consultaServico = new StatusServicoEnvio("1.00", 2);
var serializer = new XmlSerializer(typeof (StatusServicoEnvio));
using (var stream = new StreamWriter(PathEnvio))
{
serializer.Serialize(stream, consultaServico);
}
var doc = new XmlDocument();
doc.Load(PathEnvio);
doc.InnerXml = doc.InnerXml.Replace(@"<?xml version=""1.0"" encoding=""utf-8""?>", "");
doc.InnerXml =
doc.InnerXml.Replace(
@"xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""",
@"xmlns=""http://www.portalfiscal.inf.br/mdfe""");
return doc;
}
public void CriarArquivoRetorno(string xml)
{
var docRetorno = new XmlDocument();
var xmlTratado = xml.Replace(@" xmlns=""http://www.portalfiscal.inf.br/mdfe""", "");
docRetorno.LoadXml(@"<retConsStatServMDFe>" + xmlTratado + "</retConsStatServMDFe>");
var xmlWriter = new XmlTextWriter(PathRetorno, null);
xmlWriter.Formatting = Formatting.Indented;
docRetorno.Save(xmlWriter);
xmlWriter.Close();
}
public StatusServicoResposta Deserializar()
{
var deserialier = new XmlSerializer(typeof (StatusServicoResposta));
var reader = new StreamReader(PathRetorno);
var obj = deserialier.Deserialize(reader);
var resposta = (StatusServicoResposta) obj;
reader.Close();
return resposta;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment