|
-
Apr 5th, 2009, 10:49 AM
#1
Thread Starter
Hyperactive Member
Struggling with Serializing an Array of Objects. Need Help
Hi. I'm struggling with this Serializing of Data. and I've checked out the MSDN, and saw the Serializing an Array will make the out put like this:
Code:
<PurchaseOrder xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xmlns:xsd="http://www.w3.org/20001/XMLSchema">
<Items>
<Item>
<ItemID>aaa111</ItemID>
<ItemPrice>34.22</ItemPrice>
<Item>
<Item>
<ItemID>bbb222</ItemID>
<ItemPrice>2.89</ItemPrice>
<Item>
</Items>
</PurchaseOrder>
So how exactly do I do that? The only code for that on the MSDN was:
Code:
[VB.NET]
Public Class PurchaseOrder
public ItemsOrders () As Item
End Class
Public Class Item
Public ItemID As String
Public ItemPrice As decimal
End Class
Can someone please direct me a good example for doing this type of serialization?
-
Apr 5th, 2009, 02:00 PM
#2
Re: Struggling with Serializing an Array of Objects. Need Help
try it this way. i used a list(of item)
Code:
<?xml version="1.0" ?>
- <ArrayOfItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- <Item>
<ItemID>aaa111</ItemID>
<ItemPrice>34.22</ItemPrice>
</Item>
- <Item>
<ItemID>bbb222</ItemID>
<ItemPrice>2.89</ItemPrice>
</Item>
</ArrayOfItem>
vb Code:
Public Class PurchaseOrder
Public ItemsOrders As New List(Of Item)
Public Sub New()
Dim itm As New Item
itm.ItemID = "aaa111"
itm.ItemPrice = CDec(34.22)
ItemsOrders.Add(itm)
itm = New Item
itm.ItemID = "bbb222"
itm.ItemPrice = CDec(2.89)
ItemsOrders.Add(itm)
End Sub
Public Sub serialize()
Dim serializer As New Xml.Serialization.XmlSerializer(GetType(List(Of Item)))
Dim fs As New IO.FileStream("test.xml", IO.FileMode.Create)
serializer.Serialize(fs, ItemsOrders)
fs.Close()
End Sub
End Class
Public Class Item
Public ItemID As String
Public ItemPrice As Decimal
End Class
to call it:
vb Code:
Dim po As New PurchaseOrder
po.serialize()
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 5th, 2009, 02:05 PM
#3
Thread Starter
Hyperactive Member
Re: Struggling with Serializing an Array of Objects. Need Help
That's EXACTLY what I was looking for Thank You. But here's another question, Since I'm going be Editing / Appending to same XML File. How exactly would I do both?
Thanks once again.
-
Apr 5th, 2009, 02:07 PM
#4
Re: Struggling with Serializing an Array of Objects. Need Help
you'll have to deserialize it back into the list, edit the list + then serialize it again
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 5th, 2009, 02:08 PM
#5
Thread Starter
Hyperactive Member
Re: Struggling with Serializing an Array of Objects. Need Help
For Appending, would I deserialize it, Start Adding to the List, and then Resizeralize it?
-
Apr 5th, 2009, 02:09 PM
#6
Re: Struggling with Serializing an Array of Objects. Need Help
to deserialize:
vb Code:
ItemsOrders = New List(Of Item)
Dim serializer As New Xml.Serialization.XmlSerializer(GetType(List(Of Item)))
Dim SR As New IO.StreamReader("test.xml")
ItemsOrders = DirectCast(serializer.Deserialize(SR), Global.System.Collections.Generic.List(Of Item))
SR.Close()
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 5th, 2009, 02:09 PM
#7
Re: Struggling with Serializing an Array of Objects. Need Help
 Originally Posted by Wesley008
For Appending, would I deserialize it, Start Adding to the List, and then Resizeralize it?
yes.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 5th, 2009, 02:11 PM
#8
Thread Starter
Hyperactive Member
Re: Struggling with Serializing an Array of Objects. Need Help
 Originally Posted by .paul.
to deserialize:
vb Code:
ItemsOrders = New List(Of Item)
Dim serializer As New Xml.Serialization.XmlSerializer(GetType(List(Of Item)))
Dim SR As New IO.StreamReader("test.xml")
ItemsOrders = DirectCast(serializer.Deserialize(SR), Global.System.Collections.Generic.List(Of Item))
SR.Close()
WIth that code, to then call items from the XML..
I use ItemsOrder(0) or how?
-
Apr 5th, 2009, 02:18 PM
#9
Thread Starter
Hyperactive Member
Re: Struggling with Serializing an Array of Objects. Need Help
Nice. Thank You! I just messed around with it, and works perfectly.
-
Apr 5th, 2009, 02:20 PM
#10
Re: Struggling with Serializing an Array of Objects. Need Help
you just edit the list, + then when you've finished, re - serialize it
i.e.
vb Code:
ItemsOrders(0).ItemID = "new ID"
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 5th, 2009, 02:30 PM
#11
Thread Starter
Hyperactive Member
Re: Struggling with Serializing an Array of Objects. Need Help
Last Question.
My XML Output as this:
<ArrayOfItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
....
</ArrayOfItem>
How do I change the Name of That? from ArrayOfItem to say Products?
-
Apr 5th, 2009, 02:38 PM
#12
Re: Struggling with Serializing an Array of Objects. Need Help
thats the default when serializing a list.
the best you could do is changing every occurrence of Item to Products, then it'd be:
<ArrayOfProducts xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
....
</ArrayOfProducts>
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 5th, 2009, 03:53 PM
#13
Re: Struggling with Serializing an Array of Objects. Need Help
Look in MSDN for XMLArray ... and XMLArrayItem... I think that's what you want...
-tg
-
Apr 5th, 2009, 03:58 PM
#14
Thread Starter
Hyperactive Member
Re: Struggling with Serializing an Array of Objects. Need Help
hmm.. Can you give an example with my code? And are you talking about fixing the <ArrayOf...> Problem? or what?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|