XDocument.Save 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将此 XDocument 序列化为文件、文件 TextWriter或文件 XmlWriter。
重载
| 名称 | 说明 |
|---|---|
| Save(XmlWriter) | |
| Save(Stream) | |
| Save(TextWriter) |
将此序列化XDocument为 .TextWriter |
| Save(String) |
将其 XDocument 序列化为文件,覆盖现有文件(如果存在)。 |
| Save(Stream, SaveOptions) | |
| Save(TextWriter, SaveOptions) |
将此序列化 XDocument 为可选 TextWriter禁用格式设置。 |
| Save(String, SaveOptions) |
将此序列化 XDocument 为文件,可以选择禁用格式设置。 |
Save(XmlWriter)
- Source:
- XDocument.cs
- Source:
- XDocument.cs
- Source:
- XDocument.cs
- Source:
- XDocument.cs
- Source:
- XDocument.cs
public:
void Save(System::Xml::XmlWriter ^ writer);
public void Save(System.Xml.XmlWriter writer);
member this.Save : System.Xml.XmlWriter -> unit
Public Sub Save (writer As XmlWriter)
参数
示例
以下示例演示如何将 a XDocument 保存到 。XmlWriter
StringBuilder sb = new StringBuilder();
XmlWriterSettings xws = new XmlWriterSettings();
xws.OmitXmlDeclaration = true;
xws.Indent = true;
using (XmlWriter xw = XmlWriter.Create(sb, xws)) {
XDocument doc = new XDocument(
new XElement("Child",
new XElement("GrandChild", "some content")
)
);
doc.Save(xw);
}
Console.WriteLine(sb.ToString());
Dim sb As StringBuilder = New StringBuilder()
Dim xws As XmlWriterSettings = New XmlWriterSettings()
xws.OmitXmlDeclaration = True
xws.Indent = True
Using xw = XmlWriter.Create(sb, xws)
Dim doc As XDocument = New XDocument(<Child><GrandChild>some content</GrandChild></Child>)
doc.Save(xw)
End Using
Console.WriteLine(sb.ToString())
此示例生成以下输出:
<Child>
<GrandChild>some content</GrandChild>
</Child>
另请参阅
适用于
Save(Stream)
- Source:
- XDocument.cs
- Source:
- XDocument.cs
- Source:
- XDocument.cs
- Source:
- XDocument.cs
- Source:
- XDocument.cs
public:
void Save(System::IO::Stream ^ stream);
public void Save(System.IO.Stream stream);
member this.Save : System.IO.Stream -> unit
Public Sub Save (stream As Stream)
参数
注解
序列化的 XML 将缩进。 将删除所有无关紧要的空格,并添加额外的空格,以便正确缩进 XML。 此方法的行为是不会保留微不足道的空格。
如果要控制空格,请使用该Save空格的SaveOptions重载作为参数。 使用 DisableFormatting 此选项保存未输入的 XML。 这将导致编写器完全按照 XML 树中所示写入所有空格。
如果要删除重复的命名空间声明,请使用 OmitDuplicateNamespaces 选项。
适用于
Save(TextWriter)
- Source:
- XDocument.cs
- Source:
- XDocument.cs
- Source:
- XDocument.cs
- Source:
- XDocument.cs
- Source:
- XDocument.cs
将此序列化XDocument为 .TextWriter
public:
void Save(System::IO::TextWriter ^ textWriter);
public void Save(System.IO.TextWriter textWriter);
member this.Save : System.IO.TextWriter -> unit
Public Sub Save (textWriter As TextWriter)
参数
- textWriter
- TextWriter
将写入到的 TextWriter AXDocument。
示例
以下示例创建一个 XDocument文档,将文档保存到一个 StringWriter,然后将字符串打印到控制台。
StringBuilder sb = new StringBuilder();
XDocument doc = new XDocument(
new XElement("Root",
new XElement("Child", "content")
)
);
TextWriter tr = new StringWriter(sb);
doc.Save(tr);
Console.WriteLine(sb.ToString());
Dim sb As StringBuilder = New StringBuilder()
Dim doc As XDocument = _
<?xml version="1.0" encoding="utf-8"?>
<Root><Child>content</Child></Root>
Dim tr As TextWriter = New StringWriter(sb)
doc.Save(tr)
Console.WriteLine(sb.ToString())
此示例生成以下输出:
<?xml version="1.0" encoding="utf-16"?>
<Root>
<Child>content</Child>
</Root>
注解
序列化的 XML 将缩进。 将删除所有无关紧要的空格,并添加额外的空格,以便正确缩进 XML。 此方法的行为是不会保留微不足道的空格。
如果要控制空格,请使用该Save空格的SaveOptions重载作为参数。 有关详细信息,请参阅 在加载或分析 XML 时保留空白, 并在 序列化时保留空白。
另请参阅
适用于
Save(String)
- Source:
- XDocument.cs
- Source:
- XDocument.cs
- Source:
- XDocument.cs
- Source:
- XDocument.cs
- Source:
- XDocument.cs
将其 XDocument 序列化为文件,覆盖现有文件(如果存在)。
public:
void Save(System::String ^ fileName);
public void Save(string fileName);
member this.Save : string -> unit
Public Sub Save (fileName As String)
参数
- fileName
- String
一个包含文件名称的字符串。
示例
以下示例创建一个 XDocument文档,将文档保存到文件中,然后将该文件打印到控制台。
XDocument doc = new XDocument(
new XElement("Root",
new XElement("Child", "content")
)
);
doc.Save("Root.xml");
Console.WriteLine(File.ReadAllText("Root.xml"));
Dim doc As XDocument = _
<?xml version="1.0" encoding="utf-8"?>
<Root><Child>content</Child></Root>
doc.Save("Root.xml")
Console.WriteLine(File.ReadAllText("Root.xml"))
此示例生成以下输出:
<?xml version="1.0" encoding="utf-8"?>
<Root>
<Child>content</Child>
</Root>
注解
序列化的 XML 将缩进。 将删除所有无关紧要的空格,并添加额外的空格,以便正确缩进 XML。 此方法的行为是不会保留微不足道的空格。
如果要控制空格,请使用该Save空格的SaveOptions重载作为参数。 有关详细信息,请参阅 在加载或分析 XML 时保留空白, 并在 序列化时保留空白。
另请参阅
适用于
Save(Stream, SaveOptions)
- Source:
- XDocument.cs
- Source:
- XDocument.cs
- Source:
- XDocument.cs
- Source:
- XDocument.cs
- Source:
- XDocument.cs
public:
void Save(System::IO::Stream ^ stream, System::Xml::Linq::SaveOptions options);
public void Save(System.IO.Stream stream, System.Xml.Linq.SaveOptions options);
member this.Save : System.IO.Stream * System.Xml.Linq.SaveOptions -> unit
Public Sub Save (stream As Stream, options As SaveOptions)
参数
- options
- SaveOptions
一个 SaveOptions 指定格式设置行为。
注解
默认情况下,设置为 optionsNone. 此选项将删除所有无关紧要的空白,并添加适当的无关紧要的空格,以便正确缩进 XML。
如果要保存未输入的 XML,请DisableFormatting指定标记。options 这将导致编写器完全按照 XML 树中所示写入所有空格。
如果要删除重复的命名空间声明,请使用 OmitDuplicateNamespaces 选项。
适用于
Save(TextWriter, SaveOptions)
- Source:
- XDocument.cs
- Source:
- XDocument.cs
- Source:
- XDocument.cs
- Source:
- XDocument.cs
- Source:
- XDocument.cs
将此序列化 XDocument 为可选 TextWriter禁用格式设置。
public:
void Save(System::IO::TextWriter ^ textWriter, System::Xml::Linq::SaveOptions options);
public void Save(System.IO.TextWriter textWriter, System.Xml.Linq.SaveOptions options);
member this.Save : System.IO.TextWriter * System.Xml.Linq.SaveOptions -> unit
Public Sub Save (textWriter As TextWriter, options As SaveOptions)
参数
- textWriter
- TextWriter
要 TextWriter 输出 XML 到的。
- options
- SaveOptions
一个 SaveOptions 指定格式设置行为。
示例
下面的示例演示此方法的两个用法。 第一个用法对 XDocument 格式进行序列化。 第二个保留空格。 由于文档在构造中没有空格,因此保留空格会输出 XML,而无需任何缩进。
XDocument doc = new XDocument(
new XElement("Root",
new XElement("Child", "content")
)
);
StringBuilder sb1 = new StringBuilder();
using (StringWriter sr1 = new StringWriter(sb1)) {
doc.Save(sr1, SaveOptions.None);
Console.WriteLine(sb1.ToString());
}
StringBuilder sb2 = new StringBuilder();
using (StringWriter sr2 = new StringWriter(sb2)) {
doc.Save(sr2, SaveOptions.DisableFormatting);
Console.WriteLine(sb2.ToString());
}
Dim doc As XDocument = _
<?xml version="1.0" encoding="utf-8"?>
<Root><Child>content</Child></Root>
Dim sb1 As StringBuilder = New StringBuilder()
Using sr1 = New StringWriter(sb1)
doc.Save(sr1, SaveOptions.None)
Console.WriteLine(sb1.ToString())
End Using
Dim sb2 As StringBuilder = New StringBuilder()
Using sr2 = New StringWriter(sb2)
doc.Save(sr2, SaveOptions.DisableFormatting)
Console.WriteLine(sb2.ToString())
End Using
此示例生成以下输出:
<?xml version="1.0" encoding="utf-16"?>
<Root>
<Child>content</Child>
</Root>
<?xml version="1.0" encoding="utf-16"?><Root><Child>content</Child></Root>
注解
如果要保存未输入的 XML,请DisableFormatting指定标记。options 这将导致编写器编写所有空格完全如 XML 树中所示。
如果要保存缩进的 XML,请不要指定DisableFormatting该标志。options 这将删除所有无关紧要的空白,并添加适当的无关紧要的空格,以便正确缩进 XML。 这是默认行为,以及不Save用作参数的方法的options重载的行为。
有关详细信息,请参阅 在加载或分析 XML 时保留空白, 并在 序列化时保留空白。
另请参阅
适用于
Save(String, SaveOptions)
- Source:
- XDocument.cs
- Source:
- XDocument.cs
- Source:
- XDocument.cs
- Source:
- XDocument.cs
- Source:
- XDocument.cs
将此序列化 XDocument 为文件,可以选择禁用格式设置。
public:
void Save(System::String ^ fileName, System::Xml::Linq::SaveOptions options);
public void Save(string fileName, System.Xml.Linq.SaveOptions options);
member this.Save : string * System.Xml.Linq.SaveOptions -> unit
Public Sub Save (fileName As String, options As SaveOptions)
参数
- fileName
- String
一个包含文件名称的字符串。
- options
- SaveOptions
一个 SaveOptions 指定格式设置行为。
示例
下面的示例演示此方法的两个用法。 第一个用法保留空白。 第二个 XDocument 序列化为缩进。
XDocument doc = new XDocument(
new XElement("Root",
new XElement("Child", "content")
)
);
doc.Save("Root1.xml", SaveOptions.DisableFormatting);
Console.WriteLine(File.ReadAllText("Root1.xml"));
doc.Save("Root2.xml", SaveOptions.None);
Console.WriteLine(File.ReadAllText("Root2.xml"));
Dim doc As XDocument = _
<?xml version="1.0" encoding="utf-8"?>
<Root><Child>content</Child></Root>
doc.Save("Root1.xml", SaveOptions.DisableFormatting)
Console.WriteLine(File.ReadAllText("Root1.xml"))
doc.Save("Root2.xml", SaveOptions.None)
Console.WriteLine(File.ReadAllText("Root2.xml"))
此示例生成以下输出:
<?xml version="1.0" encoding="utf-8"?><Root><Child>content</Child></Root>
<?xml version="1.0" encoding="utf-8"?>
<Root>
<Child>content</Child>
</Root>
注解
如果要保存未输入的 XML,请DisableFormatting指定标记。options 这将导致编写器编写所有空格完全如 XML 树中所示。
如果要保存缩进的 XML,请不要指定DisableFormatting该标志。options 这将删除所有无关紧要的空白,并添加适当的无关紧要的空格,以便正确缩进 XML。 这是默认行为,以及不Save用作参数的方法的options重载的行为。
有关详细信息,请参阅 在加载或分析 XML 时保留空白, 并在 序列化时保留空白。