如何使用XDocument保留所有XML格式?

时间:2022-06-29 21:44:25

I'm trying to read in an XML configuration file, do a few tweaks (finding and removing or adding an element) and save it again. I want this edit to be as non-intrusive as possible since the file will be under source control and I don't want inconsequential changes to cause merge conflicts, etc. This is roughly what I've got:

我正在尝试读取XML配置文件,进行一些调整(查找和删除或添加元素)并再次保存。我希望这个编辑尽可能不干扰,因为该文件将受源代码控制,我不希望无关紧要的更改导致合并冲突,等等。这大致是我得到的:

XDocument configDoc = XDocument.Load(fileName, LoadOptions.PreserveWhitespace);
// modifications to configDoc here
configDoc.Save(fileName, SaveOptions.DisableFormatting);

There's a few problems that pop up here:

这里出现了一些问题:

  1. encoding="utf-8" gets added to the xml declaration.
  2. encoding =“utf-8”被添加到xml声明中。
  3. <tag attr="val"/> gets changed to <tag attr="val" />
  4. 变为
  5. Attributes that were spread across separate lines for readability get pushed all on to one line.
  6. 为了便于阅读而分散在不同行中的属性将全部推送到一行。

Is there any way to be less intrusive with XDocument or will I have to just try and do string editing to get what I want?

是否有任何方法可以减少对XDocument的侵扰,或者我是否必须尝试进行字符串编辑以获得我想要的内容?

2 个解决方案

#1


4  

The LINQ to XML object model does not store whether an element parsed is marked up as <foo/> or <foo /> so when saving back such information is lost. If you want to ensure a certain format then you could extend an XmlWriter implementation and override its http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter.writeendelement.aspx but that way you would also not preserve the input format, rather you would then write out any empty elements as <foo/> or whatever format you implement in your method.

LINQ to XML对象模型不存储解析的元素是否标记为 ,因此在保存时会丢失此类信息。如果你想确保某种格式,那么你可以扩展一个XmlWriter实现并覆盖它的http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter.writeendelement.aspx,但那样你也不会保留输入格式,然后你会把任何空元素写成 或你在方法中实现的任何格式。

There are other changes that can happen, for instance when loading the file

可能会发生其他更改,例如加载文件时

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:xhtml="http://www.w3.org/1999/xhtml">
  <head>
    <title>Example</title>
  </head>
  <body>
    <h1>Example</h1>
  </body>
</html>

and saving it back the result is

并将结果保存回来

<xhtml:html xmlns="http://www.w3.org/1999/xhtml" xmlns:xhtml="http://www.w3.org/1999/xhtml">
  <xhtml:head>
    <xhtml:title>Example</xhtml:title>
  </xhtml:head>
  <xhtml:body>
    <xhtml:h1>Example</xhtml:h1>
  </xhtml:body>
</xhtml:html>

so don't expect markup details to be preserved when loading/saving with XDocument/XElement.

因此,在使用XDocument / XElement加载/保存时,不要指望保留标记详细信息。

#2


1  

To avoid the declaration in the header of the document you can use the following

要避免在文档标题中声明,您可以使用以下内容

    XmlWriterSettings settings = new XmlWriterSettings();
    settings.OmitXmlDeclaration = true;


        using (XmlWriter xw = XmlWriter.Create(fileName, settings))
        {
            doc.Save(xw);
        }

#1


4  

The LINQ to XML object model does not store whether an element parsed is marked up as <foo/> or <foo /> so when saving back such information is lost. If you want to ensure a certain format then you could extend an XmlWriter implementation and override its http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter.writeendelement.aspx but that way you would also not preserve the input format, rather you would then write out any empty elements as <foo/> or whatever format you implement in your method.

LINQ to XML对象模型不存储解析的元素是否标记为 ,因此在保存时会丢失此类信息。如果你想确保某种格式,那么你可以扩展一个XmlWriter实现并覆盖它的http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter.writeendelement.aspx,但那样你也不会保留输入格式,然后你会把任何空元素写成 或你在方法中实现的任何格式。

There are other changes that can happen, for instance when loading the file

可能会发生其他更改,例如加载文件时

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:xhtml="http://www.w3.org/1999/xhtml">
  <head>
    <title>Example</title>
  </head>
  <body>
    <h1>Example</h1>
  </body>
</html>

and saving it back the result is

并将结果保存回来

<xhtml:html xmlns="http://www.w3.org/1999/xhtml" xmlns:xhtml="http://www.w3.org/1999/xhtml">
  <xhtml:head>
    <xhtml:title>Example</xhtml:title>
  </xhtml:head>
  <xhtml:body>
    <xhtml:h1>Example</xhtml:h1>
  </xhtml:body>
</xhtml:html>

so don't expect markup details to be preserved when loading/saving with XDocument/XElement.

因此,在使用XDocument / XElement加载/保存时,不要指望保留标记详细信息。

#2


1  

To avoid the declaration in the header of the document you can use the following

要避免在文档标题中声明,您可以使用以下内容

    XmlWriterSettings settings = new XmlWriterSettings();
    settings.OmitXmlDeclaration = true;


        using (XmlWriter xw = XmlWriter.Create(fileName, settings))
        {
            doc.Save(xw);
        }