.NET Framework - Leading/Trailing Whitespace with XmlReader/XmlReaderSettings

Asked By Wonko the San on 17-Feb-09 10:43 AM

I am having an issue with XamlReader trimming strings with leading or
trailing spaces when reading a ResourceDictionary.

For instance, there may be a string like this:

I thought that setting XmlReaderSettings.IgnoreWhitespace to false would
prevent this, but it doesn't.   See code snippet below.  When I check the
ResourceDictionary that gets loaded, the string is "Notice the space before
and after" instead of " Notice the space before and after ".


//Create a new StreamReader.
StreamReader sw = new StreamReader(xamlFile);

//Create an XmlReader
XmlReaderSettings readerSettings = new XmlReaderSettings();
readerSettings.CloseInput = true;
readerSettings.IgnoreProcessingInstructions = true;
readerSettings.IgnoreWhitespace = false;
readerSettings.IgnoreComments = true;
readerSettings.ProhibitDtd = true;
XmlReader xmlReader = XmlReader.Create(sw.BaseStream,

// load English resource dictionary
ResourceDictionary rd = (ResourceDictionary)

Pavel Minaev replied on 18-Feb-09 04:27 AM
On Feb 17, 7:43=A0am, Wonko the Sane <Wonko the S...@nospam.nospam>

IgnoreWhitespace is actually false by default - which is effectively
required by the XML spec:

are not markup through to the application."

so it's no surprise that it works the same whether you set it or not.
It's up to the application to decide how it handles that whitespace.
In your case, the "application" is XamlReader, and it handles it the
usual way, by stripping whitespace that is defined as insignificant by
the spec. If it did it some other way, then it wouldn't be a proper
XAML reader anymore.

If you want the spaces to be preserved in your code, either use
xml:space=3D"preserve" attribute on the str:String element, or represent
spaces with character entities -  
hongye replied on 18-Feb-09 05:51 AM
Hello Paul,

Thanks for your post. I am Hongye Sun [MSFT] and it is my pleasure to work
with you on this issue.

First of all, thanks for Pavel's answer. It is very insightful and
accurate. IgnoreWhitespace is not targeting to this issue. By specifying
xml:space="preserve" on element can solve the problem well when you intend
to apply this rule on specific elements and you are able to modify the XAML

In addition, I want to provide another way that you can set
xml:space="preserve" globally to all elements in your code instead of
changing the XAML code. Here is the modified code from your example:
//Create a new StreamReader.
StreamReader sw = new StreamReader(xamlFile);

//Create a ParserContext to specify global rule when processing
ParserContext pc = new ParserContext();
// Equals to xml:space="preserve"
pc.XmlSpace = "preserve";

// Change to Load method to load Stream and ParserContext
ResourceDictionary rd = (ResourceDictionary)XamlReader.Load(sw.BaseStream,
If you specify the "preserve" on ParserContext.XmlSpace, all the spaces in
elements will not be trimmed. Please make sure that the code will not
affect other parts of your code.
Note: the "preserve" must be all lower case. The XmlSpace property is case

XmlReaderSettings.IgnoreWhitespace has a little confusion in its name.
Actually, it means ignore the Whitespace type node, which is documented at
http://msdn.microsoft.com/en-us/library/system.xml.xmlnodetype.aspx. The
Whitespace node type means the white space between markups. When set
IgnoreWhitespace to false (default value), XmlReader.Read() method will
return Whitespace node if there is a space, tab or blank lines between
different nodes. Otherwise, it will ignore it.

Please let me know if Pavel and mine suggestions work for you. I will be
very glad to support if you have any questions or anything unclear.

Have a great day!

Hongye Sun (hongyes@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:

Get notification to my posts through email? Please refer to

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
This posting is provided "AS IS" with no warranties, and confers no rights.