locked
XmlDictionaryReader/XmlDocument and special characters RRS feed

  • Question

  • XmlDocument.Load with an XmlDictionaryReader appears to ignore a space immediately following a special character:

    const string xml = @"<Colors>Red &amp; Green</Colors>";
    
    var reader = XmlDictionaryReader.CreateTextReader(Encoding.UTF8.GetBytes(xml), new XmlDictionaryReaderQuotas());
    var doc = new XmlDocument();
    doc.Load(reader);
    
    Console.WriteLine(doc.OuterXml);

     The result of this code snippet is: <Colors>Red &amp;Green</Colors>. Note that the space after the ampersand has been removed. If I encode the ampersand with &#x26, I get the same result.

    Is this a known issue, or am I doing something wrong?

    Thanks,

    Kent

    • Moved by John Saunders Friday, June 11, 2010 8:04 PM xml q (From:ASMX Web Services and XML Serialization)
    Friday, June 11, 2010 7:56 PM

Answers

  • XmlDictionaryReader doesn't seem to be doing the right thing here.  Now before everyone gets really worried, I could not reproduce through an actual serialization with NetDataContractSerializer, fortunately!

    I think I have found a workaround.  Set doc.PreserveWhitespace = true; before doc.Load.  This should not be necessary, however, because the space after the ampersand is not "insignificant whitespace", thus is should never be ignored, regardless of this setting.

    (What is happening is that the XmlDictionaryReader is reporting a XmlNodeType.Whitespace node with value " " after the entity.  It should be XmlNodeType.SignificantWhitespace or XmlNodeType.Text.  Not sure which one is proper in this particular case, but I'm pretty sure it is not XmlNodeType.Whitespace.)

    Please report this on https://connect.microsoft.com/visualstudio and Microsoft should evaluate and respond.

    Saturday, June 12, 2010 1:17 AM

All replies

  • XmlDictionaryReader doesn't seem to be doing the right thing here.  Now before everyone gets really worried, I could not reproduce through an actual serialization with NetDataContractSerializer, fortunately!

    I think I have found a workaround.  Set doc.PreserveWhitespace = true; before doc.Load.  This should not be necessary, however, because the space after the ampersand is not "insignificant whitespace", thus is should never be ignored, regardless of this setting.

    (What is happening is that the XmlDictionaryReader is reporting a XmlNodeType.Whitespace node with value " " after the entity.  It should be XmlNodeType.SignificantWhitespace or XmlNodeType.Text.  Not sure which one is proper in this particular case, but I'm pretty sure it is not XmlNodeType.Whitespace.)

    Please report this on https://connect.microsoft.com/visualstudio and Microsoft should evaluate and respond.

    Saturday, June 12, 2010 1:17 AM
  • Monday, June 14, 2010 6:17 PM