How to xml serialize arraylist of objects?

B

Brad

I would like to serialize an arraylist of objects to xml so I can store the
xml in a database column. How would I code the serializing and
deserializing? Below is a (overly) simple, incomplete example of what I'd
want to accomplish.

Thanks
Brad


Example
=================================
' This object will be used in an arraylist
Public Class MyObject
Private _name as string

Public Property Name as string
Get
Return _name as string
End Get
Set (ByVal Value)
_name = Value
End Set
End Property
End Class

=================================
' This object will serialize xml for
Public Class BusLayer
Public Sub Update
Dim myList as new ArrayList

Dim mo1 as new MyObject
mo1.Name = "Jack"
myList.Add(mo1)

Dim mo2 as new MyObject
mo2.Name = "Jill"
myList.Add(mo2)

' Serializing code. What I've figure out so far but does not
work.

Dim mySerializer As New
System.Xml.Serialization.XmlSerializer(GetType(ArrayList))
Dim strWriter As New StringWriter
Dim writer As New XmlTextWriter(strWriter)
writer.Formatting = Formatting.Indented
mySerializer.Serialize(writer, attributeValues)

Dim result as string = strWriter.ToString
' String xml can now be written to database in stored procedure
parameter...easy to do

End Sub

Public Sub Read
Dim dbString as string

' Deserializing code here assume dbString already read from database
' and needs to be deserialized. How????


End Sub

End Class
 
C

Cor

Hi Brad,

What I would do is making a dataset with one table and one column
Filling that by itterate through that and than say
dim mystring as string ds.GetXML()
This gives a simple XML string.

I did not use this yet and I do not know yet how to deserialize, but that is
not your question.

Maybe you can try it,

I hope this helps?

Cor
 
P

Peter Huang

Hi Brad,

Thanks for posting in the community.

First of all, I would like to confirm my understanding of your issue.
From your description, I understand that you wants to serialize an
arraylist to a xml file and deserialize ti from the xml.
Have I fully understood you? If there is anything I misunderstood, please
feel free to let me know.

Here I write the code sinippet.

<code>
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim objs As New Group
Dim obj1 As New MyObject
obj1.Name = "Obj1Name"
Dim obj2 As New MyObject
obj2.Name = "Obj2Name"
objs.Info = New ArrayList
objs.Info.Add(obj1)
objs.Info.Add(obj2)
Dim x As XmlSerializer = New XmlSerializer(GetType(Group))
Dim writer As TextWriter = New StreamWriter("Test.xml")
x.Serialize(writer, objs)
writer.Close()
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Dim serializer As New XmlSerializer(GetType(Group))
' A FileStream is needed to read the XML document.
Dim fs As New FileStream("Test.xml", FileMode.Open)
Dim reader As New XmlTextReader(fs)
Dim gp As Group = CType(serializer.Deserialize(reader), Group)
For Each o As MyObject In gp.Info
MsgBox(o.Name)
Next
End Sub

Public Class Group
<XmlElement(Type:=GetType(MyObject))> _
Public Info As ArrayList
End Class
Public Class MyObject
Private _name As String
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal Value As String)
_name = Value
End Set
End Property
End Class

</code>


Also here is some links may help you.
Controlling XML Serialization Using Attributes
See section "Serializing an ArrayList"
http://longhorn.msdn.microsoft.com/lhsdk/ndp/cpconcontrollingserializationby
xmlserializerwithattributes.aspx

Examples of XML Serialization
See section: Serializing a Class that Implements the ICollection Interface
http://longhorn.msdn.microsoft.com/lhsdk/ndp/cpconanexampleofxmlserializatio
nwithxmlserializer.aspx

Please apply my suggestion above and let me know if it helps resolve your
problem.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
B

Brad

Peter, Thank you very much for your reply and for your example.
Using your example and some further reading of docs to understand I found
that I could the code would also work without the "Group" object if I
changed the following in Button1 and Button2:
From
New XmlSerializer(GetType(Group))
To
New System.Xml.Serialization.XmlSerializer(GetType(ArrayList), New
Type() {GetType(MyObject)})

Is there anything wrong with this alternate approach?

Thanks

Brad
 
B

Brad

Peter, a second followup if I may:
The function below (somewhat similar to your button2_click) works fine,
however with some traces I found instantiating the StringReader is quite
expensive, ~1.7 seconds, for simple xml, also below. That was a bit
disappointing as I expected to use this for extracting xml from multiple sql
data rows but it will be to slow for an interactive web app.

Brad

============================================
Public Function SelectedAttributes(ByVal attributes As String) As ArrayList
Dim result As ArrayList
If attributes.Length > 0 Then
Dim mySerializer As System.Xml.Serialization.XmlSerializer _
= New
System.Xml.Serialization.XmlSerializer(GetType(ArrayList) _
, New Type() _

{GetType(Catalog.Item.Details.SelectedValue)})

' This next line is what is costing a lot of time.
Dim strReader As New StringReader(attributes)

Dim reader As New XmlTextReader(strReader)
result = CType(mySerializer.Deserialize(reader), ArrayList)
Return result
End If
End Function

============================================
Xml that is the value of "attributes" and read by the stringreader

<?xml version="1.0" encoding="utf-16"?>
<ArrayOfAnyType xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<anyType xsi:type="SelectedValue">
<Attribute>Size</Attribute>
<Value>L</Value>
</anyType>
<anyType xsi:type="SelectedValue">
<Attribute>Color</Attribute>
<Value>Blue</Value>
</anyType>
</ArrayOfAnyType>
 
P

Peter Huang

Hi Brad,

Thanks for your quickly reply!

Yes, the method below is also OK.
New System.Xml.Serialization.XmlSerializer(GetType(ArrayList), New
Type() {GetType(MyObject)})

But use a goup class will benifit more flexible.

Also the stringreader seems to work fine on my side. Is the attributes
somewhat huge in your side?
' This next line is what is costing a lot of time.
Dim strReader As New StringReader(attributes)

Anyway have you tried to serialize the arraylist to an byte array and them
store it into the database. Also when you want, you can deserialize the
arraylist from the byte array, this may help you improve your performance.

Dim sm As MemoryStream
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim obj1 As New MyObject
obj1.Name = "Obj1Name"
Dim obj2 As New MyObject
obj2.Name = "Obj2Name"
Dim ar As New ArrayList
ar.Add(obj1)
ar.Add(obj2)

Dim x As XmlSerializer = New
System.Xml.Serialization.XmlSerializer(GetType(ArrayList), New Type()
{GetType(MyObject)})
sm = New MemoryStream
x.Serialize(sm, ar)
'This will write the sm to byte array, and you can store into
database.
'sm.ToArray()
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
sm.Position = 0
Dim serializer As XmlSerializer = New
XmlSerializer(GetType(ArrayList), New Type() {GetType(MyObject)})
'Here you can get the bytearray from the database and create a new memory
stream.
Dim reader As New XmlTextReader(sm)
Dim gp As ArrayList = CType(serializer.Deserialize(reader),
ArrayList)
For Each o As MyObject In gp
MsgBox(o.Name)
Next
sm.Close()
End Sub


Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top