Bug 16028 - WriteXml / ReadXml methods ignored for IXmlSerializable implementated classes
Summary: WriteXml / ReadXml methods ignored for IXmlSerializable implementated classes
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: System.XML ()
Version: 3.2.x
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2013-11-07 23:56 UTC by Aaron Henselin
Modified: 2015-05-15 04:37 UTC (History)
3 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Aaron Henselin 2013-11-07 23:56:11 UTC
Classes implementing IXmlSerizalable do not have their ReadXml / WriteXml methods read. For instance, a dictionary with custom serialization might have the method:

        public void WriteXml(XmlWriter writer)
        {
            foreach (var key in Keys)
            {
                writer.WriteStartElement(key);
                writer.WriteValue(this[key]);
                writer.WriteEndElement();
            }
        }

On .NET this produces expected results of 
<SampleDictionary>
  <OS>Windows</OS>
  <Version>95</Version>
</SampleDictionary>

On Mono, this method is ignored, and produces key value pairs using default xml serialization:
<SerializableDictionaryOfStringString xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays"><KeyValueOfstringstring><Key>OS</Key><Value>Windows</Value></KeyValueOfstringstring><KeyValueOfstringstring><Key>Version</Key><Value>95</Value></KeyValueOfstringstring><KeyValueOfstringstring></KeyValueOfstringstring></SerializableDictionaryOfStringString>


For reference, serialization is being handled through a datacontractserializer:
        public static string Serialize(Type type, object val)
        {
            if (type == null) { throw new ArgumentNullException("type"); }
            if (val == null) { throw new ArgumentNullException("val"); }


            var builder = new StringBuilder();
            var serializer = new DataContractSerializer(type);
            var xmlWriter = XmlWriter.Create(builder);
            serializer.WriteObject(xmlWriter, val);
            xmlWriter.Close();

            return builder.ToString().Replace("<?xml version=\"1.0\" encoding=\"utf-16\"?>", string.Empty)

        }
Comment 1 henrik 2014-04-23 17:48:31 UTC
+1 to fix this one; important for dealing with non-C# languages, e.g.:

/// string argument is SummaryText
type Duration =
  | Regular of string
  | Temporary of TemporaryDuration * string
  interface IXmlSerializable with
    member x.ReadXml(r : XmlReader) =
      failwith "not implemented"
    member x.WriteXml(w : XmlWriter) =
      let summary =
        match x with
        | Regular summary_text ->
          w.WriteStartElement "Regular"
          w.WriteEndElement()
          summary_text
        | Temporary(temp_dur, summary_text) ->
          w.WriteStartElement "Temporary"
          w.WriteElementString("TermLength", (int temp_dur).ToString())
          w.WriteEndElement()
          summary_text
      w.WriteElementString("SummaryText", summary)
    member x.GetSchema() =
      null
Comment 2 henrik 2014-04-23 17:52:27 UTC
Here's the line of code that is run, when it should not be run:

https://github.com/mono/mono/blob/master/mcs/class/System.XML/System.Xml.Serialization/XmlReflectionImporter.cs#L307
Comment 3 Marek Safar 2015-05-15 04:37:27 UTC
Fixed in master