设置 XmlDocument 格式(缩进)而不删除换行符

发布于 2025-01-15 00:55:29 字数 1462 浏览 3 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谎言月老 2025-01-22 00:55:29

我通常建议 LINQ to XML 是一个更好的 API。您可以插入所需的空格以保留所需的格式。例如:

var doc = XDocument.Load(inputFilePath, LoadOptions.PreserveWhitespace);

var child = doc.Elements("SomeParentElement").Elements("SomeChildElement").First();

child.AddAfterSelf(
    new XText("\r\n  "),
    new XElement("TestElement"));

doc.Save(outputFilePath);

请参阅此小提琴以获取工作演示。

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:

var doc = XDocument.Load(inputFilePath, LoadOptions.PreserveWhitespace);

var child = doc.Elements("SomeParentElement").Elements("SomeChildElement").First();

child.AddAfterSelf(
    new XText("\r\n  "),
    new XElement("TestElement"));

doc.Save(outputFilePath);

See this fiddle for a working demo.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文