XmlSerializer and built-in WhiteSpace

I have the following xml that contains an empty Field1Value field. When I deserialize this xml, I lose a single space. The value of Request.Field2 is "". Is this a bug in the XML serializer? Can someone recommend a solution / workaround to save this space?

  ...
            var encoding = new System.Text.UTF8Encoding();
            var _xmlData = "<Request><Field1>Field1Value</Field1><Field2>   </Field2></Request>";
            var _xmlDataAsByteArray = new byte[_xmlData.Length];
            _xmlDataAsByteArray = encoding.GetBytes(_xmlData);

            var _memoryStream = new MemoryStream(_xmlDataAsByteArray);

           var _XmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(Request));

            Request _request = _XmlSerializer.Deserialize(_memoryStream) as Request;

  ...
         public class Request
           {
               public string Field1;
               public string Field2;
           }
+5
source share
5 answers

No, this is not a mistake, but the expected behavior. If you do not want to save space, XML are processors (for example, reading and writing XML applications) that should normalize spaces . See Section 2.1 of the XML 1.1 specification here .

, xml:space="preserve". , XML :

<Request>
   <Field1>Field1Value</Field1>
   <!-- spaces inside Field2 will be preserved -->
   <Field2 xml:space="preserve">   </Field2> 
</Request>
+3

XmlReader xml IngoreWhitespace false

new XmlSerializer(typeof(Request)).Deserialize(XmlReader.Create(_memoryStream, new XmlReaderSettings { IgnoreWhitespace = false })) as Request;
+3

, xml: space , 'preserve'. . .

0
0

. XML I, xml: space = preserve. IXmlSerializable ( , ). , - ( / XML). WriteSettings() WriteXml(), IXmlSerializable.

    public static void WriteSettings(XmlWriter writer, Dictionary<string, string> settings)
    {
        foreach (string key in settings.Keys)
        {
            string value = settings[key];
            writer.WriteStartElement("Setting");
            writer.WriteElementString("SettingType", key);

            //writer.WriteElementString("SettingValue", value);
            // I replaced the above line, which I had previously, 
            // with the below 5 lines. 
            writer.WriteStartElement("SettingValue");
            if (value != value.Trim())
                writer.WriteAttributeString("xml", "space", null, "preserve");
            writer.WriteString(value);
            writer.WriteEndElement();

            writer.WriteEndElement();
        }
    }

XML, ( , , WriteSettings ):

<ResourceSettings>
  <Setting>
    <SettingType>SomeSettingName</SettingType>
    <SettingValue>1</SettingValue>
  </Setting>
  <Setting>
    <SettingType>AnotherSettingName</SettingType>
    <SettingValue xml:space="preserve"> </SettingValue>
  </Setting>
  <Setting>
    <SettingType>ADifferentSettingName</SettingType>
    <SettingValue>some other value</SettingValue>
  </Setting>
</ResourceSettings>

, , , XmlReader xml: space = preserve, :

    public void ReadXml(XmlReader reader)
    {
        _cache = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);
        if (reader.MoveToContent() == XmlNodeType.Element && reader.LocalName == "ResourceSettings")
        {
            // Deal with the case where there are no settings
            if (reader.ReadToDescendant("Setting"))
            {
                while (reader.MoveToContent() == XmlNodeType.Element && reader.LocalName == "Setting")
                {
                    reader.ReadStartElement("Setting");
                    string key = reader.ReadElementString("SettingType");
                    string value = reader.ReadElementString("SettingValue");
                    reader.ReadEndElement();
                    _cache.Add(key, value);
                }
            }
            reader.Read(); // move past container
        }
    }
0

All Articles