I ran into this problem awhile back but I'm not positive that the solution I
found will work in your case. Although I was specifying the type contained
within the ArrayList (using XmlArrayItem and typeof) I still received an
error. Turned out that I simply had to add a default constructor into the
class being added into the ArrayList. A simple example of the code I used
is shown below:
//Car class added to the ArrayList
public class Car {
[XmlElement("lic ense")]
public string License;
[XmlElement("col or")]
public string CarColor;
//Without this it wouldn't serialize properly
public Car() {}
public Car(string license, string color) {
License = license;
CarColor = color;
}
}
//Collection class wrapping the ArrayList
[XmlRoot("carsCo llection")]
public class Cars {
public Cars() {}
[XmlArray("cars" )]
[XmlArrayItem("c ar",typeof(Car) )]
public ArrayList CarsList = new ArrayList();
}
//Serialization class
class Class1 {
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args) {
Car car1 = new Car("1234","Bla ck");
Car car2 = new Car("4321","Blu e");
Cars cars = new Cars();
cars.CarsList.A dd(car1);
cars.CarsList.A dd(car2);
//Serialize
string path = "../../Cars.xml";
StreamWriter w = File.CreateText (path);
XmlSerializer s = new XmlSerializer(t ypeof(Cars));
s.Serialize(w,c ars);
w.Close();
StreamReader r = File.OpenText(p ath);
Cars cars2 = (Cars)s.Deseria lize(r);
r.Close();
Console.WriteLi ne(((Car)cars2. CarsList[0]).License);
Console.ReadLin e();
}
}
XML Output:
<carsCollecti on xmlns:xsd="http ://www.w3.org/2001/XMLSchema"
xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance">
<cars>
<car>
<license>1234 </license>
<color>Black</color>
</car>
<car>
<license>4321 </license>
<color>Blue</color>
</car>
</cars>
</carsCollection>
HTH,
Dan Wahlin
Wahlin Consulting
Microsoft MVP - ASP.NET and XML Web Services
http://www.xmlforasp.net
"Christophe r Pragash" <ch**********@h otmail.com> wrote in message
news:#W******** ******@TK2MSFTN GP12.phx.gbl...
Dino,
Thanks for the answer. I do understand that I could convert this into an
Array and Serialize the array, but I would like to know how to serialize
an arraylist. It would be great if you could pass on some sample code.
Thanks,
Chris
"Dino Chiesa [MSFT]" <di****@microso ft.com> wrote in message
news:eV******** ******@TK2MSFTN GP11.phx.gbl... why don't you just convert it to an array,
and serialize THAT.
"Christophe r Pragash" <ch**********@h otmail.com> wrote in message
news:Ol******** ******@TK2MSFTN GP11.phx.gbl... Hello all,
Following is the code that I'm using to serialize an arraylist. This code returns an "Unknown" Errror. Any ideas would be of great help.
<System.Xml.Ser ialization.XmlA rray(), _
System.Xml.Seri alization.XmlAr rayItem("User", GetType(User))> Public
arrUserList As New ArrayList
Private Function InitializeList( ) As ArrayList
'arrUserList = New ArrayList
Dim objUser1 As New User
objUser1.LastLo gOnDate = "01/01/1900"
objUser1.Passwo rdExpiryDate = "02/02/2002"
objUser1.UserID = "ID"
objUser1.UserNa me = "NAME"
arrUserList.Add (objUser1)
Dim objUser2 As New User
objUser2.LastLo gOnDate = "Some Log On Date"
objUser2.Passwo rdExpiryDate = "Password Expiry Date"
objUser2.UserID = "UserID"
objUser2.UserNa me = "UserName"
arrUserList.Add (objUser2)
Return arrUserList
End Function
Private Sub cmdSerialize_Cl ick(ByVal sender As System.Object, ByVal e
As System.EventArg s) Handles cmdSerialize.Cl ick
Try
Call InitializeList( )
Dim sWriter As New
System.IO.Strea mWriter("C:\Col lectionSerializ ation.xml")
Dim xmlSerial As New XmlSerializer(a rrUserList.GetT ype())
xmlSerial.Seria lize(sWriter, arrUserList.Get Type())
sWriter.Close()
Catch ex As Exception
MessageBox.Show (ex.Message & vbCrLf & ex.Source & vbCrLf &
ex.StackTrace)
End Try
End Sub
thanks,
Chris