I have a code that is passed a string containing XML. This XML may contain one or more instances of   (object reference for the space character). I have a requirement that these links not be resolved (i.e. they should not be replaced with the actual space character).
Is there any way to achieve this?
Basically, for a string containing XML:
<pattern value="[A-Z0-9 ]" />
I do not have it converted to:
<pattern value="[A-Z0-9 ]" />
(What I'm actually trying to achieve is simply to take an XML string and write it to a “pretty printed” file. This has the side effect of allowing   to be   in the string for one space, which needs to be saved. The reason for this requirement is that a written XML document must comply with a specification defined externally.)
I tried to subclass the XmlTextReader class to read from an XML string and override the ResolveEntity() method, but this is not called. I also tried to assign a custom XmlResolver .
I also tried, as suggested, "double coding." Unfortunately, this did not have the desired effect, since & not decoded by the parser. Here is the code I used:
string schemaText = @"...<pattern value=""[A-Z0-9&#x20;]"" />..."; 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 ); }
Written XML ends with:
<pattern value="[A-Z0-9&#x20;]" />
source share