Bearbeiten

Freigeben über


XStreamingElement.Save Method

Definition

Serialize this streaming element. The output can be saved to a file, an XmlTextWriter, a TextWriter, or an XmlWriter. Optionally, formatting (indenting) can be disabled.

Overloads

Save(Stream)

Outputs this XStreamingElement to the specified Stream.

Save(TextWriter)

Serialize this streaming element to a TextWriter.

Save(String)

Serialize this streaming element to a file.

Save(XmlWriter)

Serialize this streaming element to an XmlWriter.

Save(Stream, SaveOptions)

Outputs this XStreamingElement to the specified Stream, optionally specifying formatting behavior.

Save(TextWriter, SaveOptions)

Serialize this streaming element to a TextWriter, optionally disabling formatting.

Save(String, SaveOptions)

Serialize this streaming element to a file, optionally disabling formatting.

Save(Stream)

Source:
XStreamingElement.cs
Source:
XStreamingElement.cs
Source:
XStreamingElement.cs

Outputs this XStreamingElement to the specified 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)

Parameters

stream
Stream

The stream to output this XDocument to.

Remarks

The serialized XML will be indented. All insignificant white space will be removed, and additional white space will be added so that the XML will be properly indented. The behavior of this method is that insignificant white space will not be preserved.

If you want to control white space, use the overload of Save that takes SaveOptions as a parameter. Use the DisableFormatting option to save unindented XML. This will cause the writer to write all white spaces exactly as represented in the XML tree.

Use OmitDuplicateNamespaces option if you want to remove duplicate namespace declarations.

Applies to

Save(TextWriter)

Source:
XStreamingElement.cs
Source:
XStreamingElement.cs
Source:
XStreamingElement.cs

Serialize this streaming element to a 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)

Parameters

textWriter
TextWriter

A TextWriter that the XStreamingElement will be written to.

Examples

The following example creates a source XML tree, then instantiates an XStreamingElement using a query on the source XML tree. It then writes the streaming element to a 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())

This example produces the following output:

<?xml version="1.0" encoding="utf-16"?>
<NewRoot>
  <DifferentChild>3</DifferentChild>
  <DifferentChild>4</DifferentChild>
  <DifferentChild>5</DifferentChild>
</NewRoot>

Remarks

The serialized XML will be indented. All insignificant white space will be removed, and additional white space will be added so that the XML will be properly indented. The behavior of this method is that insignificant white space nodes in the XML tree will not be preserved.

If you want to control white space, use one of the overloads of Save that take SaveOptions as a parameter. For more information, see Preserve white space while loading or parsing XML and Preserve white space while serializing.

See also

Applies to

Save(String)

Source:
XStreamingElement.cs
Source:
XStreamingElement.cs
Source:
XStreamingElement.cs

Serialize this streaming element to a file.

public:
 void Save(System::String ^ fileName);
public void Save (string fileName);
member this.Save : string -> unit
Public Sub Save (fileName As String)

Parameters

fileName
String

A String that contains the name of the file.

Examples

The following example creates a streaming XML tree. It then serializes the streaming XML tree to a file.

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"))

This example produces the following output:

<?xml version="1.0" encoding="utf-8"?>
<NewRoot>
  <DifferentChild>3</DifferentChild>
  <DifferentChild>4</DifferentChild>
  <DifferentChild>5</DifferentChild>
</NewRoot>

Remarks

The serialized XML will be indented. All insignificant white space will be removed, and additional white space will be added so that the XML will be properly indented. The behavior of this method is that insignificant white space nodes in the XML tree will not be preserved.

If you want to control white space, use one of the overloads of Save that take SaveOptions as a parameter. For more information, see Preserve white space while loading or parsing XML and Preserve white space while serializing.

See also

Applies to

Save(XmlWriter)

Source:
XStreamingElement.cs
Source:
XStreamingElement.cs
Source:
XStreamingElement.cs

Serialize this streaming element to an 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)

Parameters

writer
XmlWriter

A XmlWriter that the XElement will be written to.

Examples

The following example creates an XStreamingElement and writes it to an 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())

This example produces the following output:

<NewRoot><DifferentChild>5</DifferentChild></NewRoot>

See also

Applies to

Save(Stream, SaveOptions)

Source:
XStreamingElement.cs
Source:
XStreamingElement.cs
Source:
XStreamingElement.cs

Outputs this XStreamingElement to the specified Stream, optionally specifying formatting behavior.

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)

Parameters

stream
Stream

The stream to output this XDocument to.

options
SaveOptions

A SaveOptions object that specifies formatting behavior.

Remarks

By default the options are set to None. This option will remove all extraneous insignificant white space, and add appropriate insignificant white space so that the XML is properly indented.

If you want to save unindented XML, specify the DisableFormatting flag for options. This will cause the writer to write all white spaces exactly as represented in the XML tree.

Use OmitDuplicateNamespaces option if you want to remove duplicate namespace declarations.

Applies to

Save(TextWriter, SaveOptions)

Source:
XStreamingElement.cs
Source:
XStreamingElement.cs
Source:
XStreamingElement.cs

Serialize this streaming element to a TextWriter, optionally disabling formatting.

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)

Parameters

textWriter
TextWriter

The TextWriter to output the XML to.

options
SaveOptions

A SaveOptions that specifies formatting behavior.

Examples

The following example shows two uses of this method. The first use preserves white space. The second one serializes the XStreamingElement with formatting.

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())

This example produces the following output:

<?xml version="1.0" encoding="utf-16"?><NewRoot><DifferentChild>3</DifferentChild></NewRoot>
------
<?xml version="1.0" encoding="utf-16"?>
<NewRoot>
  <DifferentChild>3</DifferentChild>
</NewRoot>

Remarks

If you want to save unindented XML, specify the DisableFormatting flag for options. This will cause the writer to write all white space exactly as represented in the XML tree.

If you want to save indented XML, do not specify the DisableFormatting flag for options. This will remove all extraneous insignificant white space, and add appropriate insignificant white space so that the XML is properly indented. This is the default behavior, and the behavior of the overloads of the Save methods that do not take options as a parameter.

For more information, see Preserve white space while loading or parsing XML and Preserve white space while serializing.

See also

Applies to

Save(String, SaveOptions)

Source:
XStreamingElement.cs
Source:
XStreamingElement.cs
Source:
XStreamingElement.cs

Serialize this streaming element to a file, optionally disabling formatting.

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)

Parameters

fileName
String

A String that contains the name of the file.

options
SaveOptions

A SaveOptions object that specifies formatting behavior.

Examples

The following example shows two uses of this method. The first use preserves white space. The second one serializes the XStreamingElement with formatting.

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"))

This example produces the following output:

<?xml version="1.0" encoding="utf-8"?><NewRoot><DifferentChild>3</DifferentChild></NewRoot>
------
<?xml version="1.0" encoding="utf-8"?>
<NewRoot>
  <DifferentChild>3</DifferentChild>
</NewRoot>

Remarks

If you want to save unindented XML, specify the DisableFormatting flag for options. This will cause the writer to write all white space exactly as represented in the XML tree.

If you want to save indented XML, do not specify the DisableFormatting flag for options. This will remove all extraneous insignificant white space, and add appropriate insignificant white space so that the XML is properly indented. This is the default behavior, and the behavior of the overloads of the Save methods that do not take options as a parameter.

For more information, see Preserve white space while loading or parsing XML and Preserve white space while serializing.

See also

Applies to