473,395 Members | 1,629 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,395 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 1323
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: 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:
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.