Serializing array of objects

This article shows you how to serialize array of objects present in the arraylist. Here objects are instances of one class.

The following code will illustrate you how to serialize array of objects present in the arraylist.Here I have Phones as class that is to serialized.It consists of arraylist which will be holding the objects of another class named Models

public class Phones{

//XmlArray("Phones") denotes root element of XML to be created. XmlArrayItem(typeof(Models)) Here Models is another class.It tells about the type of element that is to be present in the arraylist that is to be serialized

[XmlArray ("Phones"), XmlArrayItem(typeof(Models))]
public ArrayList ArrLstCell = new ArrayList();

//The following function adds Models object to arraylist

public void addObject(Models objModel){

The code for serialization.The following code will serialize class which consists of arraylist in turn which consists of objects.

//object creation for Phones class and Models class

Phones phonesObj=new Phones();
Models Model1=new Models();
Models Model2=new Models();

//Model1 and Model2 objects are added to arraylist present in the Phones class


//object creation for XmlSerializer and given its type that is to be serialized

XmlSerializer serObj = new XmlSerializer (typeof(Phones));

//Streamwriter object is used to write in the xml file
StreamWriter writerObj = new StreamWriter("C:\\PhoneDetails.xml");

//the following code will serialize the given phonesObj and write it into the xml file
//using streamwriter.


Author: suzanne haig02 May 2004 Member Level: Bronze   Points : 0

I have a question that perhaps you can answer. I have a class with a number of different types of properties.

I have an arraylist that I add these class objects to.

How do you reference these elements in the array list using the index.

Suppose there are several properties in each class type, name.

how do you rerference alist(0).type?

Author: Sandip23 Jun 2004 Member Level: Bronze   Points : 0

Hi! I am trying to pass an arry of objects having a member as another array of objects to/from web service. But, I have got serialization error.

Does anyone has any idea about how to solve this problem?

Author: Sandip23 Jun 2004 Member Level: Bronze   Points : 0

Not a problem.

I found the solution. The only thing I was supposed to do was to create and test it in a simple test application and a web service instead of testing it with hundreds of line of code for several operations!!!!!!

But, the strange thing I learned from the test result was that you don't need to do any serialization when you send an array of objects that contains another array of objects in itself as an parameter to a web service operation. But, on the other hand, if you want to return the similar kind of thing, you have to serialize it!!!

Isn't it strange??!!

Correct me if I am wrong.
Make my concepts clear if you have any idea for that.

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: