473,387 Members | 1,476 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 1322
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Waz | last post by:
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...
0
by: Tom Kent | last post by:
I have an array list of objects that I want to serialize into an XML file. I have tried this using the following code, but it gives me an error message An unhandled exception of type...
1
by: Rein Petersen | last post by:
Hi Everyone! I hope someone may have experience serializing a NameObjectCollectionBase (System.Collections.Specialized) and can advise me as to why I receive this error (below) when I try to...
0
by: Kenneth Baltrinic | last post by:
Is the following code correct for serializing a quasi-single class, that is a class that has a descreet (though more than one, so not a true singleton) number of static instances and no dynamically...
1
by: chaitanyag | last post by:
Hi, I have my data stored in a set of classes (or structs, doesn't matter), which I am trying to serialize. These classes are stored in an ArrayList, which serializes ok when all the objects in...
5
by: Victor Paraschiv | last post by:
I need to serialize into an XML file a hashtable. From MSDN at XmlSerializer:...
0
by: Jordan Bowness | last post by:
I make a similar post in another newsgroup, but this example is simplified somewhat. I have a component (cmpMyComponent) with 2 properties. The 1st property is a string value (Description) and...
0
by: Michael Maercker | last post by:
Hi! I'm about to go nuts over my serializing problem. This is my situation: I have a Data-Class that can have children of the same class which are stored in a hashtable, i.e: X has A as a...
3
by: RandomEngineer | last post by:
So here's the challenge... How can a collection (System.Collections.Generic.IList) of some custom type be serialized in a web service using .NET 2.0? Below are the class and the web methods in...
1
by: =?Utf-8?B?a2thcnJl?= | last post by:
Hi all, We decided to clear the test db more frequently and I was asked to serialize som test objects to make sure our unit testing continues to work after clearing the db. Does anyone have...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.