dotnetspider.com
Login Login    Register      

TutorialsForumCareer DevelopmentResourcesReviewsJobsInterviewCommunitiesProjectsTraining

Subscribe to Subscribers
Talk to Webmaster
Tony John

Facebook
Google+
Twitter
LinkedIn
Online Membersbaskar
More...
Join our online Google+ community for Bloggers, Content Writers and Webmasters




Resources » .NET programming » .NET Framework

Serializing array of objects


Posted Date:     Category: .NET Framework    
Author: Member Level: Silver    Points: 7


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){
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);





Did you like this resource? Share it with your friends and show your love!


Responses to "Serializing array of objects"
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      

Post Comment:




  • 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:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: Adding your own tablestyle to datagrid depending on the DataTable
    Previous Resource: Reflection - Obtaining Information about Methods at runtime
    Return to Resources
    Post New Resource
    Category: .NET Framework


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    (No tags found.)



    Follow us on Twitter: https://twitter.com/dotnetspider

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Email subscription
  • .NET Jobs
  • .NET Articles
  • .NET Forums
  • Articles Rss Feeds
    Forum Rss Feeds


    About Us    Contact Us    Copyright    Privacy Policy    Terms Of Use    Revenue Sharing sites   Advertise   Talk to Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.