Leading/Trailing Whitespace with XmlReader/XmlReaderSettings

W

Wonko the Sane

Hello,

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:
<sys:String x:Key="strSomeKey"> Notice the space before and after
</sys:String>

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 ".

Thanks,
WtS

<Code>
//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,
readerSettings);

// load English resource dictionary
ResourceDictionary rd = (ResourceDictionary)
XamlReader.Load(xmlReader);
</Code>
 
P

Pavel Minaev

Hello,

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:
<sys:String x:Key="strSomeKey"> Notice the space before and after
</sys:String>

I thought that setting XmlReaderSettings.IgnoreWhitespace to false would
prevent this, but it doesn't.

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

"An XML processor MUST always pass all characters in a document that
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="preserve" attribute on the str:String element, or represent
spaces with character entities -
 
H

Hongye Sun [MSFT]

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
code.

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,
pc);
---------------------------------
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
sensitive.

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!

Regards,
Hongye Sun ([email protected], 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:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

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
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Top