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?
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()
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.
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
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?
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()
Re: Struggling with Serializing an Array of Objects. Need Help
Quote:
Originally Posted by
Wesley008
For Appending, would I deserialize it, Start Adding to the List, and then Resizeralize it?
yes.
Re: Struggling with Serializing an Array of Objects. Need Help
Quote:
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?
Re: Struggling with Serializing an Array of Objects. Need Help
Nice. Thank You! I just messed around with it, and works perfectly. :)
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"
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?
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>
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
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?