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
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 (firstname.lastname@example.org, 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.