Note that there are some explanatory texts on larger screens.

plurals
  1. POCustom XmlSerializer issue with null elements in a List<>
    primarykey
    data
    text
    <p>I have a class that I want a custom serializer for. And I will sometimes use this class within a List, that I also want to serialize. Some of those elements will be null.</p> <p>I can get serialization working:</p> <pre><code>&lt;MyData xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt; &lt;Fractions&gt; &lt;Frac&gt;1/2&lt;/Frac&gt; &lt;Frac xsi:nil="true" /&gt; &lt;Frac xsi:nil="true" /&gt; &lt;Frac&gt;3/6&lt;/Frac&gt; &lt;/Fractions&gt; &lt;/MyData&gt; </code></pre> <p>But de-serialization is not working when null elements are present like above. The List&lt;> serializer appears to be invoking ReadXml() for a element vs. just creating a null element in the list.</p> <p>When I run my example, the de-serialized version is:</p> <pre><code>1/2 / / 3/6 </code></pre> <p>I.e. a MyFrac object was created instead of null in element 1 and 2.</p> <p>Do I have to create a custom serializer for a List sub-class to get around this, or am I missing some other way to get null elements on de-serialize? If custom serializer, any best approach/code?</p> <p>I have a complete example below which shows my current implementation.</p> <pre><code>public class MyFrac : IXmlSerializable { public string N; public string D; public override string ToString() { return N + "/" + D; } System.Xml.Schema.XmlSchema IXmlSerializable.GetSchema() { return null; } void IXmlSerializable.ReadXml(System.Xml.XmlReader reader) { if (reader.IsEmptyElement &amp;&amp; reader.NodeType != XmlNodeType.EndElement) { reader.Read(); return; } reader.ReadStartElement(); string sfrac = reader.ReadString(); try { var m = Regex.Match(sfrac, @"(\d+)/(\d+)"); if (!m.Success) throw new Exception(sfrac + " was not in the correct format"); N = m.Result("$1"); D = m.Result("$2"); } finally { reader.ReadEndElement(); } } void IXmlSerializable.WriteXml(System.Xml.XmlWriter writer) { writer.WriteString(N + "/" + D); } } public class MyData { [XmlArrayItem("Frac")] public List&lt;MyFrac&gt; Fractions; } public static void Run() { var data = new MyData(); data.Fractions = new List&lt;MyFrac&gt;(); data.Fractions.Add(new MyFrac { N = "1", D = "2" }); data.Fractions.Add(null); data.Fractions.Add(null); data.Fractions.Add(new MyFrac { N = "3", D = "6" }); var serializer = new XmlSerializer(typeof(MyData)); StringBuilder sb = new StringBuilder(); using (var writer = new StringWriter(sb)) { serializer.Serialize(writer, data); } // Dump XML Console.WriteLine(sb.ToString()); using (var reader = new StringReader(sb.ToString())) { var data2 = (MyData)serializer.Deserialize(reader); Console.WriteLine(data2.Fractions[0]); Console.WriteLine(data2.Fractions[1]); Console.WriteLine(data2.Fractions[2]); Console.WriteLine(data2.Fractions[3]); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload