SaveOptions 枚举

定义

指定序列化选项。Specifies serialization options.

此枚举有一个 FlagsAttribute 属性,允许按位组合成员值。

public enum class SaveOptions
[System.Flags]
public enum SaveOptions
type SaveOptions = 
Public Enum SaveOptions
继承
SaveOptions
属性

字段

DisableFormatting 1

串行化时保留所有无关紧要的空格。Preserve all insignificant white space while serializing.

None 0

串行化时对 XML 进行格式设置(缩进)。Format (indent) the XML while serializing.

OmitDuplicateNamespaces 2

在进行串行化时移除重复的命名空间声明。Remove the duplicate namespace declarations while serializing.

示例

下面的示例演示了两种序列化方式。The following example shows two ways to serialize. 第一种在 XML 树中保留无意义的空白区域。The first preserves insignificant white space in the XML tree. 第二个序列XElement化和格式设置。The second serializes the XElement with formatting.

string str;  
XElement root = XElement.Parse(@"<Root> <Child> Text </Child> </Root>");  
  
root.Save("Root.xml", SaveOptions.DisableFormatting);  
str = File.ReadAllText("Root.xml");  
Console.WriteLine(str);  
  
Console.WriteLine("=====");  
  
root.Save("Root.xml", SaveOptions.None);  
str = File.ReadAllText("Root.xml");  
Console.WriteLine(str);  
Dim str As String  
Dim root As XElement = <Root><Child> Text </Child></Root>  
  
root.Save("Root.xml", SaveOptions.DisableFormatting)  
str = File.ReadAllText("Root.xml")  
Console.WriteLine(str)  
  
Console.WriteLine("=====")  
  
root.Save("Root.xml", SaveOptions.None)  
str = File.ReadAllText("Root.xml")  
Console.WriteLine(str)  
  

该示例产生下面的输出:This example produces the following output:

<?xml version="1.0" encoding="utf-8"?><Root><Child> Text </Child></Root>  
=====  
<?xml version="1.0" encoding="utf-8"?>  
<Root>  
  <Child> Text </Child>  
</Root>  

注解

如果在序列化时禁用格式设置, 则 XML 树中所有无意义的空白都将按原样序列化。If you disable formatting when serializing, all insignificant white space in the XML tree is serialized as is. 如果不禁用格式设置, 则将丢弃 XML 树中所有无意义的空白, 并在序列化时对 XML 进行格式设置 (缩进)。If you do not disable formatting, then all insignificant white space in the XML tree is discarded, and the XML is formatted (indented) as it is serialized.

此类型使你能够控制在序列化时 LINQ to XML 处理空白的方式。This type allows you to control how LINQ to XML handles white space when serializing. 有关详细信息, 请参阅在加载或分析 XML 时保留空白, 并在序列化时保留空白区域For more information, see Preserving White Space while Loading or Parsing XML and Preserving White Space While Serializing.

适用于

另请参阅