471,073 Members | 1,362 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,073 software developers and data experts.

Serializing Collections

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);
}
}
Nov 22 '05 #1
1 1249
Hi Waz,

Use ArrayList for delivery list and it should work.

--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com

"Waz" <wc*******@inspire.net.nz> wrote in message
news:04****************************@phx.gbl...
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);
}
}

Nov 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Waz | last post: by
reply views Thread by Tom Kent | last post: by
reply views Thread by Kenneth Baltrinic | last post: by
1 post views Thread by chaitanyag | last post: by
5 posts views Thread by Victor Paraschiv | last post: by
reply views Thread by Michael Maercker | last post: by
1 post views Thread by =?Utf-8?B?a2thcnJl?= | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.