Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / July 2003

Tip: Looking for answers? Try searching our database.

Serializing an ArrayList into XML File

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Christopher Pragash - 31 Jul 2003 18:58 GMT
Hello all,

I'm trying to persist an object of type Arraylist into XML using the
following the XMLSerializer and it returns an "Unknown" Error. I'm able to
serialize and deserialize the arraylist object using Binary format but
unable to do so using XML formatter. Is it possible to serialize an
ArrayList object into XML? If somebody could pass on some sample code that
would be really great.

Thanks,
Chris
Brad Quinn - 31 Jul 2003 20:16 GMT
I'm new to XML in .NET, but here goes.

The XmlSerializer doesn't know what to do with the ArrayList elements.
You can convert the the ArrayList to an array of objects and annotate that
array with attributes so that the XmlSerializer will know what to do.

There may be a more conise way of doing this, but I don't know what it is;

using System;

using System.Collections;
using System.Xml.Serialization;

namespace arraylist
{
 public class DriversLicense
 {
   public string state;
   public string number;

   public DriversLicense() {
     this.state = null;
     this.number = null;
   }
   public DriversLicense( string state, string number ) {
     this.state = state;
     this.number = number;
   }
 }

 public class SocialSecurityNumber
 {
   public string number;

   public SocialSecurityNumber() {
     number = null;
   }
   public SocialSecurityNumber( string number ) {
     this.number = number;
   }
 }

 public class Identification
 {
   [XmlElementAttribute("SocialSecurityNumber",
typeof(SocialSecurityNumber))]
   [XmlElementAttribute("DriversLicense", typeof(DriversLicense))]
   public object[] Items;
 }

 class Class1
 {
   [STAThread]
   static void Main(string[] args)
   {
     ArrayList al = new ArrayList();
     al.Add( new SocialSecurityNumber( "333-22-4444" ) );
     al.Add( new DriversLicense( "NY", "1234567890" ) );

     Identification ident = new Identification();
     ident.Items = al.ToArray();

     XmlSerializer ser = new XmlSerializer( typeof(Identification) );
     ser.Serialize( Console.Out, ident );

     Console.ReadLine();
   }
 }
}

> Hello all,
>
[quoted text clipped - 7 lines]
> Thanks,
> Chris

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.