Help! IXmlSerializable Deserializing List of Custom Objects
- From: "eric.olstad@xxxxxxxxx" <eric.olstad@xxxxxxxxx>
- Date: 2 Mar 2006 09:34:39 -0800
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?
.
- Follow-Ups:
- Re: Help! IXmlSerializable Deserializing List of Custom Objects
- From: eric.olstad@xxxxxxxxx
- Re: Help! IXmlSerializable Deserializing List of Custom Objects
- Prev by Date: Marshalling type conversion
- Next by Date: Re: Reduce the number og colors in an image?
- Previous by thread: Marshalling type conversion
- Next by thread: Re: Help! IXmlSerializable Deserializing List of Custom Objects
- Index(es):
Relevant Pages
|