473,748 Members | 11,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Serializing an ArrayList

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
Nov 11 '05 #1
4 7829
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

Nov 11 '05 #2
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


Nov 11 '05 #3
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



Nov 11 '05 #4
check out the thread "Serializat ion and Arraylist" from August 5th.

"Christophe r Pragash" <ch**********@h otmail.com> wrote in message
news:%2******** ********@TK2MSF TNGP12.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



Nov 11 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2127
by: Pure Krome | last post by:
How do i serialize two instances (of some object) in the same class? I'm not sure how i would de-serialize a class that has two or more instances of some object type, and how the deserialize method would KNOW which serialized instance to read. eg.
1
12641
by: Christopher Pragash | last post by:
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,
0
2825
by: Tom Kent | last post by:
I have an array list of objects that I want to serialize into an XML file. I have tried this using the following code, but it gives me an error message An unhandled exception of type 'System.InvalidOperationException' occurred in system.xml.dl Additional information: There was an error generating the XML document I'm not sure if this is the way that I should go about serializing an array of objects like this, but I would like to get them...
3
2556
by: Daniel Lidström | last post by:
Hello, I want to have a class that contains only a collection of another class. For example: public __gc class Alignment { public: Alignment(); ... };
1
1687
by: dotNetDave | last post by:
I'm trying to create xml seriaizable collection class (below), but the xml keeps coming out wrong. In the resulting xml from the web service (below) the "ArrayOfAlarmProcessor" tag should really be "Processors" and the "AlarmProcessor" tag should be "Processor". <ArrayOfAlarmProcessor xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://mycompany/webservice/NetworkAlarms">...
0
1332
by: Ante Smolcic | last post by:
Hi all, I have an ArrayList that contains items of type A. I declared the XmlArrayItem atribute for that type. Now I have an derived type B (from A) also contained in the ArrayList but I get an error when serializing. Can this be made without redeclaring the ArrayList special attributes? The problem is that the class B is in different namespace!
2
1733
by: Q. John Chen | last post by:
I posted this in C# group and just found this spot. I created a class that implements ICollection. After serializing got following result: <?xml version=\"1.0\" encoding=\"utf-16\"> <ArrayOfCustomer> <Customer> <FirstName>John</FirstName>
0
1213
by: Yofnik | last post by:
Is there a way to avoid serializing an empty ArrayList? I would like the following class: public class Collection { public ArrayList Items = new ArrayList(); } ....to serialize like this:
2
2720
by: Phil Galey | last post by:
I'm using VB.NET 2002 on Windows 2000 Pro and am having trouble serializing a SortedList. This is my class: <Serializable()Public Class clsGoodFiles Inherits SortedList Public Sub New() MyBase.New() End Sub End Class
0
8989
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8828
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9537
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9319
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9243
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8241
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6795
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4599
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4869
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.