C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Serializing array of objects


Posted Date: 06 Mar 2004    Resource Type: Articles    Category: .NET Framework
Author: MUNIKUMAR RMember Level: Silver    
Rating: Points: 7




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){
ArrLstCell.items.add(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

phonesObj.addObject(Model1);
phonesObj.addObject(Model2);

//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.
serObj.Serialize(writerObj,phonesObj);




Responses

Author: suzanne haig    02 May 2004Member 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: Sandip    23 Jun 2004Member 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: Sandip    23 Jun 2004Member 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.


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Adding your own tablestyle to datagrid depending on the DataTable
Previous Resource: Reflection - Obtaining Information about Methods at runtime
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use