XamlWriter.Save Method

Definition

Returns XAML either directly or through an object intermediary that serializes the specified object.

Overloads

Save(Object)

Returns a XAML string that serializes the specified object and its properties.

Save(Object, Stream)

Saves XAML information into a specified stream to serialize the specified object and its properties.

Save(Object, TextWriter)

Saves XAML information as the source for a provided TextWriter object. The output of the TextWriter can then be used to serialize the provided object and its properties.

Save(Object, XamlDesignerSerializationManager)

Saves XAML information into a custom serializer. The output of the serializer can then be used to serialize the provided object and its properties.

Save(Object, XmlWriter)

Saves XAML information as the source for a provided XmlWriter object. The output of the XmlWriter can then be used to serialize the provided object and its properties.

Save(Object)

Returns a XAML string that serializes the specified object and its properties.

public static string Save (object obj);

Parameters

obj
Object

The element to be serialized. Typically, this is the root element of a page or application.

Returns

A XAML string that can be written to a stream or file. The logical tree of all elements that fall under the provided obj element will be serialized.

Exceptions

obj is null.

The application is not running in full trust.

Examples

The following example serializes a Button into a string using the XamlWriter class. The string is then deserialized back into a Button using the static Load method on the XamlReader class.

// Create the Button.
Button originalButton = new Button();
originalButton.Height = 50;
originalButton.Width = 100;
originalButton.Background = Brushes.AliceBlue;
originalButton.Content = "Click Me";

// Save the Button to a string.
string savedButton = XamlWriter.Save(originalButton);

// Load the button
StringReader stringReader = new StringReader(savedButton);
XmlReader xmlReader = XmlReader.Create(stringReader);
Button readerLoadButton = (Button)XamlReader.Load(xmlReader);

Remarks

The serialization enabled by this method has a series of limitations. This is because the serialization enabled is explicitly run-time, and does not have access to possible design-time information in the original XAML (if any). For details, see Serialization Limitations of XamlWriter.Save.

Calling Save is not permitted when running in partial trust.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

Save(Object, Stream)

Saves XAML information into a specified stream to serialize the specified object and its properties.

public static void Save (object obj, System.IO.Stream stream);

Parameters

obj
Object

The element to be serialized. Typically, this is the root element of a page or application.

stream
Stream

Destination stream for the serialized XAML information.

Exceptions

obj or stream is null.

The application is not running in full trust.

Examples

The following example serializes a Button into a MemoryStream using the XamlWriter class. The stream is then deserialized back into a Button using the static Load method on the XamlReader class.

// Create the Button.
Button originalButton = new Button();
originalButton.Height = 50;
originalButton.Width = 100;
originalButton.Background = Brushes.AliceBlue;
originalButton.Content = "Click Me";

// Save the Button to a string.
string savedButton = XamlWriter.Save(originalButton);

// Load the button
StringReader stringReader = new StringReader(savedButton);
XmlReader xmlReader = XmlReader.Create(stringReader);
Button readerLoadButton = (Button)XamlReader.Load(xmlReader);

Remarks

The serialization enabled by this method has a series of limitations. This is because the serialization enabled is explicitly run-time, and does not have access to possible design-time information in the original XAML (if any). For details, see Serialization Limitations of XamlWriter.Save.

Calling Save is not permitted when running in partial trust.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

Save(Object, TextWriter)

Saves XAML information as the source for a provided TextWriter object. The output of the TextWriter can then be used to serialize the provided object and its properties.

public static void Save (object obj, System.IO.TextWriter writer);

Parameters

obj
Object

The element to be serialized. Typically, this is the root element of a page or application.

writer
TextWriter

A TextWriter instance as the destination where the serialized XAML information is written.

Exceptions

obj or writer is null.

The application is not running in full trust.

Remarks

The serialization enabled by this method has a series of limitations. This is because the serialization enabled is explicitly run-time, and does not have access to possible design-time information in the original XAML (if any). For details, see Serialization Limitations of XamlWriter.Save.

Calling Save is not permitted when running in partial trust.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

Save(Object, XamlDesignerSerializationManager)

Saves XAML information into a custom serializer. The output of the serializer can then be used to serialize the provided object and its properties.

public static void Save (object obj, System.Windows.Markup.XamlDesignerSerializationManager manager);

Parameters

obj
Object

The element to be serialized. Typically, this is the root element of a page or application.

manager
XamlDesignerSerializationManager

A custom serialization implementation.

Exceptions

obj or manager is null.

The application is not running in full trust.

Remarks

The serialization enabled by this method has a series of limitations. This is because the serialization enabled is explicitly run-time, and does not have access to possible design-time information in the original XAML (if any). For details, see Serialization Limitations of XamlWriter.Save.

Calling Save is not permitted when running in partial trust.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

Save(Object, XmlWriter)

Saves XAML information as the source for a provided XmlWriter object. The output of the XmlWriter can then be used to serialize the provided object and its properties.

public static void Save (object obj, System.Xml.XmlWriter xmlWriter);

Parameters

obj
Object

The element to be serialized. Typically, this is the root element of a page or application.

xmlWriter
XmlWriter

Writer to use to write the serialized XAML information.

Exceptions

obj or xmlWriter is null.

The application is not running in full trust.

Remarks

The serialization enabled by this method has a series of limitations. This is because the serialization enabled is explicitly run-time, and does not have access to possible design-time information in the original XAML (if any). For details, see Serialization Limitations of XamlWriter.Save.

Calling Save is not permitted when running in partial trust.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9