保存XML而不格式化 [英] Save XML without formatting
问题描述
我需要生成xml,但是没有跳线,生成的方式都是缩进的,如何保存而不跳线?
I need to generate the xml, but without jumping line, the way I generate it is all indented, how do I save without jumping 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);
但是,SaveOptions选项没有出现,我使用的是ASP.NET CORE.
However, the SaveOptions option does not appear, I use ASP.NET CORE.
这样,数据如下所示:
<Rps>
<IdentificacaoRps>
<Numero>1</Numero>
<Serie>999</Serie>
<Tipo>1</Tipo>
</IdentificacaoRps>
<DataEmissao>2018-11-27</DataEmissao>
<Status>1</Status>
</Rps>
我需要他们像这样离开.
And I need them to leave like this.
<Rps><IdentificacaoRps><Numero>1</Numero><Serie>999</Serie><Tipo>1</Tipo</IdentificacaoRps><DataEmissao>2018-11-27</DataEmissao<Status>1</Status></Rps>
如何解决这个问题,有什么办法吗?
How to solve this problem, is there any way?
推荐答案
为有效地删除空白,在这里,我使用 PreserveWhiteSpace = false
XmlDocument >.从文件中加载文档后,您可以从 doc.InnerXml
中访问没有空格的XML,然后可以将其用于任何目的,但是在这里,我坚持这个问题并编写将其保存到文件中,并正确处理编写器.
To efficiently remove the white-space, here I'm creating a new XmlDocument
with PreserveWhiteSpace = false
. Once you load the document from the file, you can access the white space free XML from doc.InnerXml
, and then you can use that for whatever purpose, but here I'm sticking with the question and writing it to a file, and properly disposing the writer.
using (var writer = System.IO.File.CreateText("C:\\b.xml"))
{
var doc = new XmlDocument {PreserveWhitespace = false};
doc.Load("C:\\a.xml");
writer.WriteLine(doc.InnerXml);
writer.Flush();
}
这篇关于保存XML而不格式化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!