Whitespace in XAML

I just ran into a problem with XAML (WPF and Silverlight) and wanted to share my solution with you.  In my Word to XAML converter application, I was noticing that sometimes extraneous spaces were being seen in Paragraph elements in WPF and TextBlock elements in Silverlight.

The problem was caused because I formatted my XAML with indentations so that it would look nice like this:

 <Paragraph>
    <Run Text="gym"/>
    <Run Text="nasium"/>
</Paragraph>

It turns out that elements that use the InlineCollection like Paragraph and TextBlock interpret invisible newline characters as spaces so that XAML would show the text “gym nasium”.  See the article on MSDN.

To get the XAML to display the text correctly, you should not have spaces or newlines between elements in a Paragraph or TextBlock like this:

 <Paragraph>
    <Run Text="gym"/><Run Text="nasium"/>
</Paragraph>

This is typically not a problem when writing your own XAML but when you are creating XAML programmatically via XSLT, you can run into this issue.

I put my comment in the MSDN article (see the bottom) so that others can be helped by my experience.  Have you ever added a comment to an MSDN article?  Do you read the community comments in MSDN?

Comments

  • Anonymous
    January 30, 2009
    Oh my Gosh… Hard to believe this sort of thing still kills some of our code. Thanks to Michael for pointing

  • Anonymous
    January 30, 2009
    In the blog post I just wrote , I found a peculiarity of XAML (both WPF and Silverlight) where nicely

  • Anonymous
    January 30, 2009
    In the blog post I just wrote , I found a peculiarity of XAML (both WPF and Silverlight) where nicely

  • Anonymous
    January 30, 2009
    In this issue: Fredrik Normén, Michael S. Scherotter(2), Gerard Leblanc, Lee, and Jesse Liberty. Shoutouts

  • Anonymous
    January 31, 2009
    The comment has been removed