This documentation is archived and is not being maintained.

XamlReader Class

Updated: February 2009

Provides the means to parse XAML markup into an object or a tree of objects.

Namespace:  System.Windows.Markup
Assembly:  PresentationFramework (in PresentationFramework.dll)

public class XamlReader

XamlReader is technically instantiatable as an object in XAML because it has a default constructor. However, you generally do not do this because the only useful members of the type are methods, and the type itself has no UI purpose.

The synchronous Load methods are static, but the asynchronous LoadAsync methods are not static and require an instance of the XamlReader class.

Objects that are created by XamlReader generally then should be added to the existing object tree. Otherwise the object is considered disconnected, which means that it does not render, and cannot be accessed using any of the object tree techniques as applied to the main tree (FindName, LogicalTreeHelper, VisualTreeHelper). For more information on object tree concepts, see Trees in WPF.

XamlReader supports the following primary scenarios:

  • Cloning/object factory: Without additional mechanisms, a reference type generally cannot be included in more than one position in a WPF object tree. (Examples of additional mechanisms that offer support for sharing or re-use include objects that are based on Freezable, or a shareable object type that is referenced as an item from a ResourceDictionary.) One way to clone an object that is already in the tree is to serialize the object using XamlWriter.Save, then use the serialized string as input for a call to Load, using a stream or XmlReader as intermediary.

  • Constructing objects based on just-in-time information: There are often other ways to have late-binding or user-supplied input change the state of existing objects. For example you could use simple property sets, or data binding. But if you have a scenario where even the type of object to create is only determinable at run-time or real-time with user interaction, then creating such an object by building up a string for Load input is often a useful technique.

  • Using existing resource techniques: The Stream type is used frequently in other frameworks or technologies for transferring data or objects. You can then use these pathways to store or obtain the data in XAML form that you eventually use to create an object as part of your application.

  • Fixed documents: Your application might load local or downloaded XPS documents for inclusion in a WPF application object tree and UI.

The following example converts a Button into a string using the XamlWriter class. The string is then loaded 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);


Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0




February 2009

Added remarks about usage and scenarios.

Information enhancement.

July 2008

Added new members: Parse(String) method, Parse(String, ParserContext) method.

SP1 feature change.