Results 1 to 14 of 14

Thread: Struggling with Serializing an Array of Objects. Need Help

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    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?

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    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:
    1. Public Class PurchaseOrder
    2.     Public ItemsOrders As New List(Of Item)
    3.  
    4.     Public Sub New()
    5.         Dim itm As New Item
    6.         itm.ItemID = "aaa111"
    7.         itm.ItemPrice = CDec(34.22)
    8.         ItemsOrders.Add(itm)
    9.         itm = New Item
    10.         itm.ItemID = "bbb222"
    11.         itm.ItemPrice = CDec(2.89)
    12.         ItemsOrders.Add(itm)
    13.     End Sub
    14.  
    15.     Public Sub serialize()
    16.         Dim serializer As New Xml.Serialization.XmlSerializer(GetType(List(Of Item)))
    17.         Dim fs As New IO.FileStream("test.xml", IO.FileMode.Create)
    18.         serializer.Serialize(fs, ItemsOrders)
    19.         fs.Close()
    20.     End Sub
    21.  
    22. End Class
    23.  
    24. Public Class Item
    25.     Public ItemID As String
    26.     Public ItemPrice As Decimal
    27. End Class

    to call it:

    vb Code:
    1. Dim po As New PurchaseOrder
    2. po.serialize()

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    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.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    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

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    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?

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Struggling with Serializing an Array of Objects. Need Help

    to deserialize:

    vb Code:
    1. ItemsOrders = New List(Of Item)
    2. Dim serializer As New Xml.Serialization.XmlSerializer(GetType(List(Of Item)))
    3. Dim SR As New IO.StreamReader("test.xml")
    4. ItemsOrders = DirectCast(serializer.Deserialize(SR), Global.System.Collections.Generic.List(Of Item))
    5. SR.Close()

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Struggling with Serializing an Array of Objects. Need Help

    Quote Originally Posted by Wesley008 View Post
    For Appending, would I deserialize it, Start Adding to the List, and then Resizeralize it?
    yes.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: Struggling with Serializing an Array of Objects. Need Help

    Quote Originally Posted by .paul. View Post
    to deserialize:

    vb Code:
    1. ItemsOrders = New List(Of Item)
    2. Dim serializer As New Xml.Serialization.XmlSerializer(GetType(List(Of Item)))
    3. Dim SR As New IO.StreamReader("test.xml")
    4. ItemsOrders = DirectCast(serializer.Deserialize(SR), Global.System.Collections.Generic.List(Of Item))
    5. SR.Close()
    WIth that code, to then call items from the XML..

    I use ItemsOrder(0) or how?

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: Struggling with Serializing an Array of Objects. Need Help

    Nice. Thank You! I just messed around with it, and works perfectly.

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    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:
    1. ItemsOrders(0).ItemID = "new ID"

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    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?

  12. #12
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    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>

  13. #13
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    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
  •  



Click Here to Expand Forum to Full Width