Parsing an XML string containing " " (which must be preserved)

Posted by Zoodor on Stack Overflow See other posts from Stack Overflow or by Zoodor
Published on 2010-05-04T10:30:29Z Indexed on 2010/05/04 10:38 UTC
Read the original article Hit count: 156

Filed under:
|

I have code that is passed a string containing XML. This XML may contain one or more instances of   (an entity reference for the blank space character). I have a requirement that these references should not be resolved (i.e. they should not be replaced with an actual space character).

Is there any way for me to achieve this?

Basically, given a string containing the XML:

<pattern value="[A-Z0-9&#x20;]" />

I do not want it to be converted to:

<pattern value="[A-Z0-9 ]" />

(What I am actually trying to achieve is to simply take an XML string and write it to a "pretty-printed" file. This is having the side-effect of resolving occurrences of &#x20; in the string to a single space character, which need to be preserved. The reason for this requirement is that the written XML document must conform to an externally-defined specification.)

I have tried creating a sub-class of XmlTextReader to read from the XML string and overriding the ResolveEntity() method, but this isn't called. I have also tried assigning a custom XmlResolver.

© Stack Overflow or respective owner

Related posts about .net-2.0

Related posts about xml-parsing