设置 XmlDocument 格式(缩进)而不删除换行符
我有一个 XML 文件,我想要修改(添加新元素)然后保存。问题是该文件有不应删除的空行。
这就是我当前正在做的事情:
// loading document with PreserveWhiteSpace = true
var doc = new XmlDocument { PreserveWhitespace = true };
doc.Load(inputFilePath);
// add a new element to the document
var el = doc.CreateElement("TestElement");
doc.InsertAfter(el, doc["SomeParentElement"]["SomeChildElement"]);
// save document
var settings = new XmlWriterSettings
{
Indent = true,
IndentChars = @" ",
NewLineChars = "\r\n",
NewLineHandling = NewLineHandling.Replace,
OmitXmlDeclaration = true
};
using (var writer = XmlWriter.Create(outputFilePath, settings))
{
doc.Save(writer);
}
我在加载之前将文档 PreserveWhitespace
设置为 true,以便换行符不会被忽略。因此,我在 doc["SomeParentElement"]["SomeChildElement"] 之后添加的新元素不会在新行上开始,并且如果我自己添加换行符,则不会正确的缩进。 我尝试了 XmlWriterSetting 中的许多设置,但是当 PreserveWhitespace
设置为 true 时,其中似乎没有任何设置起作用。
是否可以将新元素插入 xml 文件并以良好的格式保存,但不删除文档中的空行?
示例:
<SomeParentElement>
<SomeChildElement/>
<SomeChildElement2/>
<SomeChildElement3/>
</SomeParentElement>
运行上面的代码后应该如下所示:
<SomeParentElement>
<SomeChildElement/>
<TestElement/>
<SomeChildElement2/>
<SomeChildElement3/>
</SomeParentElement>
I have an XML file that I want to modify (add new elements) and then save. The problem is that the file has empty lines that should not be deleted.
This is what I'm currently doing:
// loading document with PreserveWhiteSpace = true
var doc = new XmlDocument { PreserveWhitespace = true };
doc.Load(inputFilePath);
// add a new element to the document
var el = doc.CreateElement("TestElement");
doc.InsertAfter(el, doc["SomeParentElement"]["SomeChildElement"]);
// save document
var settings = new XmlWriterSettings
{
Indent = true,
IndentChars = @" ",
NewLineChars = "\r\n",
NewLineHandling = NewLineHandling.Replace,
OmitXmlDeclaration = true
};
using (var writer = XmlWriter.Create(outputFilePath, settings))
{
doc.Save(writer);
}
I'm setting the documents PreserveWhitespace
to true before loading, so that the line breaks do not get ignored. Because of this, the new element I'm adding after the doc["SomeParentElement"]["SomeChildElement"]
does not start on a new line and, if I add the newline myself, does not have the correct indentation.
I tried many of the settings in XmlWriterSetting, but nothing in there seemed to work when PreserveWhitespace
is set to true.
Is it possible to insert a new element into an xml file and save it with a nice formatting but without deleting empty lines in the document?
Example:
<SomeParentElement>
<SomeChildElement/>
<SomeChildElement2/>
<SomeChildElement3/>
</SomeParentElement>
Should look like this after running the code above:
<SomeParentElement>
<SomeChildElement/>
<TestElement/>
<SomeChildElement2/>
<SomeChildElement3/>
</SomeParentElement>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通常建议 LINQ to XML 是一个更好的 API。您可以插入所需的空格以保留所需的格式。例如:
请参阅此小提琴以获取工作演示。
I'd generally suggest LINQ to XML is a far nicer API. You can insert the whitespace you need to keep the formatting you want. For example:
See this fiddle for a working demo.