W
Waz
I'm having some problems serializing a class that contains
a collection. Creating the TestClass below and then
calling its Serialize method throws the error:
There was an error reflecting 'TestApp.TestClass'
If I use place an [XmlIgnore] over the TheDeliveryList
property it works fine. Another option may be to implement
the IXmlSerializable interface but I was hoping that this
would not be necessary.
Any help would be much appreciated.
public class TestClass {
private DeliveryList deliveryList;
private int testClassID;
public TestClass(){
deliveryList = new DeliveryList();
Delivery del = new Delivery();
del.DeliveryID="1";
deliveryList.Add(del);
del = new Delivery();
del.DeliveryID="2";
deliveryList.Add(del);
}
public int TestClassID{
get{return testClassID;}
set{testClassID=value;}
}
[XmlArray("Deliveries")]
[XmlArrayItem("Delivery", typeof(Delivery))]
public DeliveryList TheDeliveryList{
get{return deliveryList;}
set{deliveryList=value;}
}
public string Serialize(){
XmlSerializer xmlSer = new XmlSerializer(typeof
(TestClass));
StringWriter sr = new StringWriter();
xmlSer.Serialize(sr,this);
return sr.ToString();
}
}
public class Delivery{
private string deliveryID;
public string DeliveryID{
get{return deliveryID;}
set{deliveryID=value;}
}
}
public class DeliveryList : CollectionBase{
public int Add(Delivery del){
return List.Add(del);
}
}
a collection. Creating the TestClass below and then
calling its Serialize method throws the error:
There was an error reflecting 'TestApp.TestClass'
If I use place an [XmlIgnore] over the TheDeliveryList
property it works fine. Another option may be to implement
the IXmlSerializable interface but I was hoping that this
would not be necessary.
Any help would be much appreciated.
public class TestClass {
private DeliveryList deliveryList;
private int testClassID;
public TestClass(){
deliveryList = new DeliveryList();
Delivery del = new Delivery();
del.DeliveryID="1";
deliveryList.Add(del);
del = new Delivery();
del.DeliveryID="2";
deliveryList.Add(del);
}
public int TestClassID{
get{return testClassID;}
set{testClassID=value;}
}
[XmlArray("Deliveries")]
[XmlArrayItem("Delivery", typeof(Delivery))]
public DeliveryList TheDeliveryList{
get{return deliveryList;}
set{deliveryList=value;}
}
public string Serialize(){
XmlSerializer xmlSer = new XmlSerializer(typeof
(TestClass));
StringWriter sr = new StringWriter();
xmlSer.Serialize(sr,this);
return sr.ToString();
}
}
public class Delivery{
private string deliveryID;
public string DeliveryID{
get{return deliveryID;}
set{deliveryID=value;}
}
}
public class DeliveryList : CollectionBase{
public int Add(Delivery del){
return List.Add(del);
}
}