XStreamingElement.Save 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
序列化此流元素。 输出可以保存到文件、文件 XmlTextWriter、输出 TextWriter或文件 XmlWriter。 (可选)可以禁用格式设置(缩进)。
重载
| 名称 | 说明 |
|---|---|
| Save(Stream) |
将此 XStreamingElement 输出到指定的 Stream。 |
| Save(TextWriter) |
将此流式处理元素序列化为 .TextWriter |
| Save(String) |
将此流式处理元素序列化为文件。 |
| Save(XmlWriter) |
将此流式处理元素序列化为 .XmlWriter |
| Save(Stream, SaveOptions) |
将此 XStreamingElement 输出到指定的 Stream,可以选择指定格式设置行为。 |
| Save(TextWriter, SaveOptions) |
将此流式处理元素序列化为可选 TextWriter禁用格式设置。 |
| Save(String, SaveOptions) |
将此流式处理元素序列化为文件,可以选择禁用格式设置。 |
Save(Stream)
- Source:
- XStreamingElement.cs
- Source:
- XStreamingElement.cs
- Source:
- XStreamingElement.cs
- Source:
- XStreamingElement.cs
- Source:
- XStreamingElement.cs
将此 XStreamingElement 输出到指定的 Stream。
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:
- XStreamingElement.cs
- Source:
- XStreamingElement.cs
- Source:
- XStreamingElement.cs
- Source:
- XStreamingElement.cs
- Source:
- XStreamingElement.cs
将此流式处理元素序列化为 .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 AXStreamingElement。
示例
以下示例创建源 XML 树,然后实例化 XStreamingElement 对源 XML 树的查询。 然后,它将流式处理元素写入到 .StringWriter
XElement srcTree = new XElement("Root",
new XElement("Child", 1),
new XElement("Child", 2),
new XElement("Child", 3),
new XElement("Child", 4),
new XElement("Child", 5)
);
XStreamingElement dstTree = new XStreamingElement("NewRoot",
from el in srcTree.Elements()
where (int)el >= 3
select new XElement("DifferentChild", (int)el)
);
StringBuilder sb = new StringBuilder();
dstTree.Save(new StringWriter(sb));
Console.WriteLine(sb.ToString());
Dim srcTree As XElement = _
<Root>
<Child>1</Child>
<Child>2</Child>
<Child>3</Child>
<Child>4</Child>
<Child>5</Child>
</Root>
Dim dstTree As XStreamingElement = New XStreamingElement("NewRoot", _
From el In srcTree.Elements() _
Where el.Value >= 3 _
Select <DifferentChild><%= el.Value %></DifferentChild> _
)
Dim sb As StringBuilder = New StringBuilder()
dstTree.Save(New StringWriter(sb))
Console.WriteLine(sb.ToString())
此示例生成以下输出:
<?xml version="1.0" encoding="utf-16"?>
<NewRoot>
<DifferentChild>3</DifferentChild>
<DifferentChild>4</DifferentChild>
<DifferentChild>5</DifferentChild>
</NewRoot>
注解
序列化的 XML 将缩进。 将删除所有无关紧要的空格,并添加额外的空格,以便正确缩进 XML。 此方法的行为是不会保留 XML 树中微不足道的空白节点。
如果要控制空格,请使用其中Save一个作为参数的重载SaveOptions。 有关详细信息,请参阅 在加载或分析 XML 时保留空白, 并在 序列化时保留空白。
另请参阅
适用于
Save(String)
- Source:
- XStreamingElement.cs
- Source:
- XStreamingElement.cs
- Source:
- XStreamingElement.cs
- Source:
- XStreamingElement.cs
- Source:
- XStreamingElement.cs
将此流式处理元素序列化为文件。
public:
void Save(System::String ^ fileName);
public void Save(string fileName);
member this.Save : string -> unit
Public Sub Save (fileName As String)
参数
示例
以下示例创建一个流式处理 XML 树。 然后,它将流式处理 XML 树序列化为文件。
XElement srcTree = new XElement("Root",
new XElement("Child", 1),
new XElement("Child", 2),
new XElement("Child", 3),
new XElement("Child", 4),
new XElement("Child", 5)
);
XStreamingElement dstTree = new XStreamingElement("NewRoot",
from el in srcTree.Elements()
where (int)el >= 3
select new XElement("DifferentChild", (int)el)
);
dstTree.Save("Test.xml");
Console.WriteLine(File.ReadAllText("Test.xml"));
Dim srcTree As XElement = _
<Root>
<Child>1</Child>
<Child>2</Child>
<Child>3</Child>
<Child>4</Child>
<Child>5</Child>
</Root>
Dim dstTree As XStreamingElement = New XStreamingElement("NewRoot", _
From el In srcTree.Elements() _
Where el.Value >= 3 _
Select <DifferentChild><%= el.Value %></DifferentChild> _
)
dstTree.Save("Test.xml")
Console.WriteLine(File.ReadAllText("Test.xml"))
此示例生成以下输出:
<?xml version="1.0" encoding="utf-8"?>
<NewRoot>
<DifferentChild>3</DifferentChild>
<DifferentChild>4</DifferentChild>
<DifferentChild>5</DifferentChild>
</NewRoot>
注解
序列化的 XML 将缩进。 将删除所有无关紧要的空格,并添加额外的空格,以便正确缩进 XML。 此方法的行为是不会保留 XML 树中微不足道的空白节点。
如果要控制空格,请使用其中Save一个作为参数的重载SaveOptions。 有关详细信息,请参阅 在加载或分析 XML 时保留空白, 并在 序列化时保留空白。
另请参阅
适用于
Save(XmlWriter)
- Source:
- XStreamingElement.cs
- Source:
- XStreamingElement.cs
- Source:
- XStreamingElement.cs
- Source:
- XStreamingElement.cs
- Source:
- XStreamingElement.cs
将此流式处理元素序列化为 .XmlWriter
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)
参数
示例
以下示例创建一个 XStreamingElement 并将其写入到一个 XmlWriter。
XElement srcTree = new XElement("Root",
new XElement("Child", 1),
new XElement("Child", 2),
new XElement("Child", 3),
new XElement("Child", 4),
new XElement("Child", 5)
);
StringBuilder sb = new StringBuilder();
XmlWriterSettings xws = new XmlWriterSettings();
xws.OmitXmlDeclaration = true;
using (XmlWriter xw = XmlWriter.Create(sb, xws))
{
XStreamingElement dstTree = new XStreamingElement("NewRoot",
from el in srcTree.Elements()
where (int)el == 5
select new XElement("DifferentChild", (int)el)
);
dstTree.Save(xw);
}
Console.WriteLine(sb.ToString());
Dim srcTree As XElement = _
<Root>
<Child>1</Child>
<Child>2</Child>
<Child>3</Child>
<Child>4</Child>
<Child>5</Child>
</Root>
Dim sb As StringBuilder = New StringBuilder()
Dim xws As XmlWriterSettings = New XmlWriterSettings()
xws.OmitXmlDeclaration = True
Using xw As XmlWriter = XmlWriter.Create(sb, xws)
Dim dstTree As XStreamingElement = New XStreamingElement("NewRoot", _
From el In srcTree.Elements() _
Where el.Value = 5 _
Select <DifferentChild><%= el.Value %></DifferentChild> _
)
dstTree.Save(xw)
End Using
Console.WriteLine(sb.ToString())
此示例生成以下输出:
<NewRoot><DifferentChild>5</DifferentChild></NewRoot>
另请参阅
适用于
Save(Stream, SaveOptions)
- Source:
- XStreamingElement.cs
- Source:
- XStreamingElement.cs
- Source:
- XStreamingElement.cs
- Source:
- XStreamingElement.cs
- Source:
- XStreamingElement.cs
将此 XStreamingElement 输出到指定的 Stream,可以选择指定格式设置行为。
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:
- XStreamingElement.cs
- Source:
- XStreamingElement.cs
- Source:
- XStreamingElement.cs
- Source:
- XStreamingElement.cs
- Source:
- XStreamingElement.cs
将此流式处理元素序列化为可选 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 指定格式设置行为。
示例
下面的示例演示此方法的两个用法。 第一个用法保留空白。 第二个序列化 XStreamingElement 格式。
XElement srcTree = new XElement("Root",
new XElement("Child", 1),
new XElement("Child", 2),
new XElement("Child", 3),
new XElement("Child", 4),
new XElement("Child", 5)
);
XStreamingElement dstTree = new XStreamingElement("NewRoot",
from el in srcTree.Elements()
where (int)el == 3
select new XElement("DifferentChild", (int)el)
);
StringBuilder sb = new StringBuilder();
dstTree.Save(new StringWriter(sb), SaveOptions.DisableFormatting);
Console.WriteLine(sb.ToString());
Console.WriteLine("------");
sb = new StringBuilder();
dstTree.Save(new StringWriter(sb), SaveOptions.None);
Console.WriteLine(sb.ToString());
Dim srcTree As XElement = _
<Root>
<Child>1</Child>
<Child>2</Child>
<Child>3</Child>
<Child>4</Child>
<Child>5</Child>
</Root>
Dim dstTree As XStreamingElement = New XStreamingElement("NewRoot", _
From el In srcTree.Elements() _
Where el.Value = 3 _
Select <DifferentChild><%= el.Value %></DifferentChild> _
)
Dim sb As StringBuilder = New StringBuilder()
dstTree.Save(New StringWriter(sb), SaveOptions.DisableFormatting)
Console.WriteLine(sb.ToString())
Console.WriteLine("------")
sb = New StringBuilder()
dstTree.Save(New StringWriter(sb), SaveOptions.None)
Console.WriteLine(sb.ToString())
此示例生成以下输出:
<?xml version="1.0" encoding="utf-16"?><NewRoot><DifferentChild>3</DifferentChild></NewRoot>
------
<?xml version="1.0" encoding="utf-16"?>
<NewRoot>
<DifferentChild>3</DifferentChild>
</NewRoot>
注解
如果要保存未输入的 XML,请DisableFormatting指定标记。options 这将导致编写器编写所有空格完全如 XML 树中所示。
如果要保存缩进的 XML,请不要指定DisableFormatting该标志。options 这将删除所有无关紧要的空白,并添加适当的无关紧要的空格,以便正确缩进 XML。 这是默认行为,以及不Save用作参数的方法的options重载的行为。
有关详细信息,请参阅 在加载或分析 XML 时保留空白, 并在 序列化时保留空白。
另请参阅
适用于
Save(String, SaveOptions)
- Source:
- XStreamingElement.cs
- Source:
- XStreamingElement.cs
- Source:
- XStreamingElement.cs
- Source:
- XStreamingElement.cs
- Source:
- XStreamingElement.cs
将此流式处理元素序列化为文件,可以选择禁用格式设置。
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)
参数
- options
- SaveOptions
一个 SaveOptions 指定格式设置行为的对象。
示例
下面的示例演示此方法的两个用法。 第一个用法保留空白。 第二个序列化 XStreamingElement 格式。
XElement srcTree = new XElement("Root",
new XElement("Child", 1),
new XElement("Child", 2),
new XElement("Child", 3),
new XElement("Child", 4),
new XElement("Child", 5)
);
XStreamingElement dstTree = new XStreamingElement("NewRoot",
from el in srcTree.Elements()
where (int)el == 3
select new XElement("DifferentChild", (int)el)
);
dstTree.Save("Test1.xml", SaveOptions.DisableFormatting);
dstTree.Save("Test2.xml", SaveOptions.None);
Console.WriteLine(File.ReadAllText("Test1.xml"));
Console.WriteLine("------");
Console.WriteLine(File.ReadAllText("Test2.xml"));
Dim srcTree As XElement = _
<Root>
<Child>1</Child>
<Child>2</Child>
<Child>3</Child>
<Child>4</Child>
<Child>5</Child>
</Root>
Dim dstTree As XStreamingElement = New XStreamingElement("NewRoot", _
From el In srcTree.Elements() _
Where el.Value = 3 _
Select <DifferentChild><%= el.Value %></DifferentChild> _
)
dstTree.Save("Test1.xml", SaveOptions.DisableFormatting)
dstTree.Save("Test2.xml", SaveOptions.None)
Console.WriteLine(File.ReadAllText("Test1.xml"))
Console.WriteLine("------")
Console.WriteLine(File.ReadAllText("Test2.xml"))
此示例生成以下输出:
<?xml version="1.0" encoding="utf-8"?><NewRoot><DifferentChild>3</DifferentChild></NewRoot>
------
<?xml version="1.0" encoding="utf-8"?>
<NewRoot>
<DifferentChild>3</DifferentChild>
</NewRoot>
注解
如果要保存未输入的 XML,请DisableFormatting指定标记。options 这将导致编写器编写所有空格完全如 XML 树中所示。
如果要保存缩进的 XML,请不要指定DisableFormatting该标志。options 这将删除所有无关紧要的空白,并添加适当的无关紧要的空格,以便正确缩进 XML。 这是默认行为,以及不Save用作参数的方法的options重载的行为。
有关详细信息,请参阅 在加载或分析 XML 时保留空白, 并在 序列化时保留空白。