Save XML without formatting

Asked

Viewed 192 times

1

I need to generate the xml, but without skipping line, the way I Gero he is all identado, as I do to save without skipping the line?

document.LoadXml(soapEnvelope);
document.Save(@"E:\nota.xml");

I tried this code below:

XDocument document = XDocument.Load("arquivo.xml");
document.Save("arquivo2.xml", SaveOptions.DisableFormatting);

But the option does not appear SaveOptions, utilise ASP.NET CORE. This is the whole method:

private void montaEnvelope(HttpWebRequest webRequest, XmlDocument document)
{
     string soapEnvelope = string.Empty;
     soapEnvelope += "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ws=\"http://ws.issweb.fiorilli.com.br/\" xmlns:xd=\"http://www.w3.org/2000/09/xmldsig#\">";
     soapEnvelope += "<soapenv:Header/><soapenv:Body><ws:gerarNfse>";
     soapEnvelope += document.LastChild.OuterXml.Replace("<?xml version=\"1.0\" encoding=\"utf-8\"?>", string.Empty);
     soapEnvelope += "<username>01001001000113</username><password>123456</password></ws:gerarNfse></soapenv:Body></soapenv:Envelope>";

     MemoryStream stream = stringToStream(soapEnvelope);
     webRequest.ContentLength = stream.Length;
     Stream requestStream = webRequest.GetRequestStream();
     stream.WriteTo(requestStream);

     document.LoadXml(soapEnvelope);
     document.Save(@"E:\nota.xml");
     XDocument document1 = XDocument.Load(@"E:\nota.xml");
     this.XmlDocNFSe = document;
}
  • Are you sure? Because it’s inside the namespace System.Xml.Linq, you added?

  • @Virgilionovic when I put System.Xml.Linq he lets me put the SaveOptions.DisableFormatting, but it returns this Error No overload for "Save" method takes 2 arguments

  • Too weird ... !

  • @Virgilionovic just edited with the entire function, so you can see how is being declared the document. Thank you.

  • Which version of netframework?

  • @Virgilionovic uses 4.7.2 and the core is 2.1

  • Xmldocument doesn’t even have you already right the class name

  • I don’t know what you mean.

  • In your parameter is Xmldocument which is different from Xdocument you are missing in classes where Xdocument really has the overload with Saveoptions already Xmldocument does not have this overload !!!

Show 4 more comments

1 answer

2


See you are confused the classes, the one you are using by the method parameter is XmlDocument that does not overload with the parameter SaveOptions, but, there is a way to save the XmlDocument unformatted:

Example:

XmlDocument doc = new XmlDocument();

using(XmlTextWriter wr = new XmlTextWriter(fileName, Encoding.UTF8))
{
    wr.Formatting = Formatting.None;
    doc.Save(wr);
}

Reference code: Is there any way to save an Xmldocument without indentation and line Returns?

That is to say, XDocument is the class that has an overload with SaveOptions, different in what is in your code.

Reference: Is there any way to save an Xmldocument without indentation and line Returns?

Browser other questions tagged

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