Note that there are some explanatory texts on larger screens.

plurals
  1. POParsing an XML string containing " " (which must be preserved)
    primarykey
    data
    text
    <p>I have code that is passed a <code>string</code> containing XML. This XML may contain one or more instances of <code>&amp;#x20;</code> (an entity reference for the blank space character). I have a requirement that these references should not be resolved (i.e. they should <em>not</em> be replaced with an actual space character).</p> <p>Is there any way for me to achieve this?</p> <p>Basically, given a string containing the XML:</p> <pre><code>&lt;pattern value="[A-Z0-9&amp;#x20;]" /&gt; </code></pre> <p>I do <strong>not</strong> want it to be converted to:</p> <pre><code>&lt;pattern value="[A-Z0-9 ]" /&gt; </code></pre> <p>(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 <code>&amp;#x20;</code> 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.)</p> <p>I have tried creating a sub-class of <code>XmlTextReader</code> to read from the XML string and overriding the <code>ResolveEntity()</code> method, but this isn't called. I have also tried assigning a custom <code>XmlResolver</code>.</p> <p>I have also tried, as suggested, to "double encode". Unfortunately, this has not had the desired effect, as the <code>&amp;amp;</code> is <em>not</em> decoded by the parser. Here is the code I used:</p> <pre><code>string schemaText = @"...&lt;pattern value=""[A-Z0-9&amp;#x26;#x20;]"" /&gt;..."; XmlWriterSettings writerSettings = new XmlWriterSettings(); writerSettings.Indent = true; writerSettings.NewLineChars = Environment.NewLine; writerSettings.Encoding = Encoding.Unicode; writerSettings.CloseOutput = true; writerSettings.OmitXmlDeclaration = false; writerSettings.IndentChars = "\t"; StringBuilder writtenSchema = new StringBuilder(); using ( StringReader sr = new StringReader( schemaText ) ) using ( XmlReader reader = XmlReader.Create( sr ) ) using ( TextWriter tr = new StringWriter( writtenSchema ) ) using ( XmlWriter writer = XmlWriter.Create( tr, writerSettings ) ) { XPathDocument doc = new XPathDocument( reader ); XPathNavigator nav = doc.CreateNavigator(); nav.WriteSubtree( writer ); } </code></pre> <p>The written XML ends up with:</p> <pre><code>&lt;pattern value="[A-Z0-9&amp;amp;#x20;]" /&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload