Help! IXmlSerializable Deserializing List of Custom Objects

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



I uncovered this issue that I can't quite pin down. I created a test
program to test serializing and deserializing a List<> of custom
objects. The custom object in the test case is just a simple object,
but the actual object will be much more complicated. Here's the simple
class

class TestObject : IXmlSerializable
{
private string key;
private string val;

public TestObject() { }

public string Key {get {...} set{...}}
public string Value {get{...} set{...}

public void ReadXml(XmlReader reader)
{
reader.Read();
reader.ReadStartElement();
key = reader.ReadString();
reader.ReadEndElement();
reader.ReadStartElement();
val = reader.ReadString();
reader.ReadEndElement();
}
public void WriteXml(XmlWriter writer)
{
writer.WriteStartElement("Key");
writer.WriteString(key);
writer.WriteEndElement();
writer.WriteStartElement("Value");
writer.WriteString(val);
writer.WriteEndElement();
}
}

So, using the XmlSerializer, I can serialize and deserialize this class
to and from xml with no problems. If I create a list of this class
like so, List<TestObject>, and serialize the list to XML, it will be
fine. When I try to deserialize the XML List, it only calls the
ReadXml method of TestObject once, not once per item. Why?

Now a solution for this trivial example is to get rid of the
IXmlSerializable implementation and it will work fine. Why does it not
work when I implement ReadXml explicitly?

.



Relevant Pages

  • Re: version compatibility when serializing and deserializing
    ... to deserialize an object that lacks that member, ... file with the serialized object and I didn't get any exception. ... You would need to serialize an ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Serialize/DeSerialize Generics List
    ... a bit of a brain fart there: I said "Deserialize" when I meant ... "Serialize" a bunch of times. ... from Array. ... converted to-and-from your hidden List property (converting from lists ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: To Serialize or Not to Serialize?
    ... > I have been using the Help topics on Serialize and Deserialize to ... I keyed in the VB samples and the Serialize ... But if you try to feed the output XML file created ... > with the Serialize sample to the Deserialize sample, ...
    (microsoft.public.dotnet.languages.vb)
  • newbie: How to use WriteXmlSchema?
    ... I want to serialize a DataSet to local disk when my .NET 2.0 WinForms app closes, and deserialize the DataSet when the app reopens. ... How can I adjust my serialization code to include the proper information/formatting in the xml file? ...
    (microsoft.public.dotnet.xml)
  • Re: Can you serialize nested objects to XML?
    ... deserialize the object the properties simply read the state data. ... Also, for private members, couldn't you provide a custom serialize method to ... Serializing nested objects is not a problem. ... private string _Name = String.Empty; ...
    (microsoft.public.dotnet.general)