1

我有一个格式如下的 XDocument:

<tag1><innertag Name="sample">This is text</innertag></tag1>

加载并保存后更改为以下格式:

<tag1>
   <innertag Name="sample">This is text</innertag>
</tag1>

如何保留上述格式?

如果我SaveOptions在保存期间使用,则没有缩进并且整个文件在一行中。我希望保留上述格式。可能吗 ?

4

1 回答 1

1

XDocumentobject 有几个.Save()方法重载,其中一半以SaveOptions枚举值作为第二个参数。这个枚举可能有你需要的东西:

public enum SaveOptions
{
    /// <summary>Format (indent) the XML while serializing.</summary>
    None,
    /// <summary>Preserve all insignificant white space while serializing.</summary>
    DisableFormatting,
    /// <summary>Remove the duplicate namespace declarations while serializing.</summary>
    OmitDuplicateNamespaces
}
于 2018-10-04T15:02:18.717 回答