Serialize everything in a single line

Asked

Viewed 34 times

0

I am generating an xml of an Nfse, but I need everything generated in a single line, even when generating the signature. When I serialize, I do it this way:

StringWriter sw = new StringWriter();
            XmlTextWriter tw = new XmlTextWriter(sw);

            XmlSerializerNamespaces xsn = new XmlSerializerNamespaces();

            XmlSerializer ser = new XmlSerializer(typeof(GerarNfseEnvio));
            FileStream arquivo = new FileStream("E:\\nota.xml", FileMode.CreateNew);
            xsn.Add("", "http://www.abrasf.org.br/nfse.xsd");
            arquivo.Flush();
            ser.Serialize(arquivo, gerar, xsn);
            arquivo.Close();

But it is identado and in several lines, I need everything in a single line, because the way I am doing, I need to create a new file for each change, and I do this way:

using (var writer = System.IO.File.CreateText("E:\\notasemesp.xml"))
            {
                var doc = new XmlDocument { PreserveWhitespace = false };
                doc.Load("E:\\nota.xml");
                writer.WriteLine(doc.InnerXml);
                writer.Flush();
            }

Only that it is impracticable, create a new file, for new change, since until the end is made 3 changes, then would be necessary to create another 3 xml, how can I fix this problem? Example: The way I serialize it is this way:

<Rps>
  <IdentificacaoRps>
     <Numero>1</Numero>
      <Serie>999</Serie>
      <Tipo>1</Tipo>
  </IdentificacaoRps>
 <DataEmissao>2018-11-27</DataEmissao>
<Status>1</Status>

I need every change I use Save, stay that way:

 <Rps><IdentificacaoRps><Numero>1</Numero><Serie>999</Serie><Tipo>1</Tipo</IdentificacaoRps><DataEmissao>2018-11-27</DataEmissao<Status>1</Status></Rps>

1 answer

1


One option is to use XDocument of System.Xml.Linq

 System.Xml.Linq.XDocument doc = System.Xml.Linq.XDocument.Load("E:\\nota.xml");
 doc.Save("E:\\notasemesp.xml", System.Xml.Linq.SaveOptions.DisableFormatting);
  • It worked fine, thank you.

Browser other questions tagged

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