XmlReader breaks spaces after ampersand?

This is strange. I have a WCF Message and am trying to read the contents of a body in an XmlDocument. The content of the message body looks like this on the wiring (when checking with WCF trace):

<abc>
    <timeZone>(GMT-05:00) Eastern Time (US &amp; Canada)</timeZone>
</abc>

The code for the reader is as follows:

XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreWhitespace = false;
settings.CheckCharacters = false;
XmlReader bodyReader = XmlReader.Create(
        message.GetReaderAtBodyContents().ReadSubtree(), settings);
XmlDocument messageDoc = new XmlDocument();
messageDoc.Load(bodyReader);

The resulting XML in is messageDocas follows:

<abc>
    <timeZone>(GMT-05:00) Eastern Time (US &Canada)</timeZone>
</abc>

So where were the extra spaces after the original made &amp;?

+3
source share
1 answer

You can simplify the code by deleting XmlReader. Then set PreserveWhiteSpace to XmlDocument. You can replace all your code:

XmlDocument messageDoc = new XmlDocument() { PreserveWhitespace = true };
messageDoc.Load(message.GetReaderAtBodyContents().ReadSubtree());
+3
source

Source: https://habr.com/ru/post/1789293/


All Articles