Skip to content

Xaml Pretty Printing

September 8, 2006

I’m doing a bit of work on Persisting Settings.  My sample (which I’ll publish in the coming weeks) allows you to use the Setttings object or XamlWriter for persistance.

XamlWriter, by default, loses most whitespace between elements when you write out XAML for an object.  In order to get around that behavior, if you value readable XAML, you can pass in an XmlWriter with Indent set to True on the XmlWriterSettings.

Code sample:

//XamlWrite settingsObject to settings.xaml

//Create a new StreamWriter. This file will be relative to the .exe
StreamWriter sw = new StreamWriter("Settings.xaml");

//Create an XmlWriter to pretty print the XAML and
// close the stream when the xmlWriter is closed.
XmlWriterSettings writerSettings = new XmlWriterSettings();
writerSettings.Indent = true;
writerSettings.CloseOutput = true;
XmlWriter xmlWriter = XmlWriter.Create(sw.BaseStream, writerSettings);

//Save the settingsObject
XamlWriter.Save(settingsObject, xmlWriter);


Result without Pretty Print: (all on one line)

<?xml version="1.0" encoding="utf-8"?><ResourceDictionary xmlns="" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:x=""><s:Double x:Key="Window1Window1Left">700</s:Double><s:Double x:Key="Window1Window1Top">631</s:Double><WindowState x:Key="Window1Window1WindowState">Normal</WindowState><s:Double x:Key="WindowWidth">270</s:Double><s:Double x:Key="WindowHeight">238</s:Double><s:Double x:Key="Age">36</s:Double><s:String x:Key="FirstName">Rob</s:String></ResourceDictionary>

Result with Pretty Print:

<?xml version="1.0" encoding="utf-8"?>
<ResourceDictionary xmlns="" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:x="">
    <s:Double x:Key="Age">36</s:Double>
    <s:Double x:Key="WindowWidth">270</s:Double>
    <WindowState x:Key="Window1Window1WindowState">Normal</WindowState>
    <s:Double x:Key="WindowHeight">238</s:Double>
    <s:String x:Key="FirstName">Rob</s:String>
    <s:Double x:Key="Window1Window1Left">700</s:Double>
    <s:Double x:Key="Window1Window1Top">631</s:Double>

Sorry! I guess that we didn’t pick a very good default here.

Related Links from the WPF Docs and MSDN:


From → XAML (non-UI)

  1. Guy permalink

    Hi what if the xaml Is custom control that need to work on and xbap ?


    And in the code behind I want to initialized this xaml and set a new handler for it.
    so …
    using System.Windows.Shapes;
    namespace rotatedCube
    public partial class rotateCube


    Control _rotateCube;
    public rotateCube()
    _rotateCube = new rotateCube();
    System.IO.Stream s = this.GetType().Assembly.
    //_rotateCube = this.
    //_rotateCube = xml . xa xaml initiliz(new System.IO.StreamReader(s).ReadToEnd());

    // Insert code required on object creation below this point.


    Got a little lost in the initialization of the xaml , and…
    with did the :
    public partial class rotateCube  // need to be inherited || could be inherited from window class
    and still work in xbap ?
    Or he don\’t need be inherited at all ?
    Or he need to be inhered from control ?
    Please any way to arrange this a little bit ?

  2. Unknown permalink

    wow gold!All wow gold US Server 24.99$/1000G on sell! Cheap wow gold,wow gold,wow gold,Buy Cheapest/Safe/Fast WoW US EU wow gold Power leveling wow gold from the time you wWorld of Warcraft gold ordered!

    wow power leveling wow power leveling power leveling wow power leveling wow powerleveling wow power levelingcheap wow power leveling wow power leveling buy wow power leveling wow power leveling buy power leveling wow power leveling cheap power leveling wow power leveling wow power leveling wow power leveling wow powerleveling wow power leveling power leveling wow power leveling wow powerleveling wow power leveling buy rolex cheap rolex wow gold wow gold wow gold wow gold -145024216737397

  3. Unknown permalink

    Rolex Watch, Breitling
    Replica Watch, Best
    replica watches, Omega
    Replica Watches ecosway的朋友小聚,很愜意

  4. Unknown permalink

    Welcome to enter (wow gold) and (wow power leveling) trading site, (wow gold) are cheap, (wow power leveling) credibility Very good! Quickly into the next single! Key words directly to the website click on transactions! -144798873306510

  5. Unknown permalink

    Amberdigital Branch,Southern Stars Enterprises Co is specializing in the development and manufacturing of ad players, advertisement player and LCD advertisings. Established in 1996, we have explored and developed the international market with professionalism. We have built a widespread marketing network, and set up a capable management team dedicated to provide beyond-expectation services to our customers.

    amberdigital Contact Us
    Southern Stars Enterprises Co (Hong Kong Office)
    Add:3 Fl, No.2, Lane 2, Kam Tsin Tsuen, Sheung Shui, Hong Kong
    Tel:+852 2681 4099
    Fax:+852 2681 4586
    Southern Stars Enterprises Co (Shenzhen Office)
    Add:DE, 16/F, Building 2, Nanguo Tower, Sungang Road, Shenzhen, China
    Tel:+86 755 2592 9100
    Fax:+86 755 2592 7171[aif

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: