none
XML serialization from class RRS feed

  • Question

  • Hi all,

    Hope somebody can help me with this.

    We have an XSD schema file used to import xml orders into our system. We have been having some problems with data and I was given the task to make the XML order submission strong type. So, what I did was to use the "xsd.exe" to create a vb.net class based on the xsd schema to use in our application. Now what I need to do is serialize the class into XML. Since the order class contains a header and details, I am having some trouble adding the details and serialize the entire class.

    Within the order class, a property for the items was created as an array like this.

     <System.Xml.Serialization.XmlElementAttribute("item")> _
        Public Property item() As POitem()
            Get
                Return Me.itemField
            End Get
            Set(value As POitem())
                Me.itemField = value
            End Set
        End Property

    So, I got the header to serialize fine like this.

    Dim objStreamWriter As New StreamWriter("C:\Orders.xml")
    Dim pos As New purchaseorders

    pos.emailto = "test@test.com"
    pos.processed = "False"


    Dim x As New XmlSerializer(pos.GetType) x.Serialize(objStreamWriter, pos) objStreamWriter.Close()


    My question is how do I add the items to the header and serialize the header with all the details?

    Thanks in advance.

    Tuesday, December 31, 2013 9:18 PM

Answers

  • XmlSerializer will handle serializing the properties based upon the attributes applied to the type.  All you need to do is set the properties on the object.

    pos.item = //Set to your array

    It is no different then when you are setting the array property of any other type.  The serializer will handle the serialization of the array elements for you.  Note however that POitem must also be serializable otherwise an exception will occur (if this was part of your XSD then it should have been autogenerated for you).

    Michael Taylor
    http://msmvps.com/blogs/p3net

    • Marked as answer by admdev Thursday, January 2, 2014 7:02 PM
    Tuesday, December 31, 2013 9:42 PM
    Moderator

All replies

  • XmlSerializer will handle serializing the properties based upon the attributes applied to the type.  All you need to do is set the properties on the object.

    pos.item = //Set to your array

    It is no different then when you are setting the array property of any other type.  The serializer will handle the serialization of the array elements for you.  Note however that POitem must also be serializable otherwise an exception will occur (if this was part of your XSD then it should have been autogenerated for you).

    Michael Taylor
    http://msmvps.com/blogs/p3net

    • Marked as answer by admdev Thursday, January 2, 2014 7:02 PM
    Tuesday, December 31, 2013 9:42 PM
    Moderator
  • Hi Michael,

    Thanks for your reply. So this is how I am doing it and it's working great. Obviously, in live data the "Dim po(10) As PurchaseOrder" will be the number of orders I need to create. 

     Dim objStreamWriter As New StreamWriter("C:\Orders.xml")
     Dim pos As New PurchaseOrders
     Dim po(10) As PurchaseOrder
    
    '=====================================
    'Create the POs XML Element
    '=====================================
    pos.emailto = "test@test.com"
    pos.processed = "False"
    pos.xmlCreated = "Yes"
    
    '==================================
    'Add the PO record to the P.Os file
    '==================================
    Dim p As New PurchaseOrder()
    
    p.ponum = "44455"
    p.billCode = billCode.R
    p.custno = "TEST11"
    
    po(0) = p
    pos.purchaseorder = po
    
    Dim x As New XmlSerializer(pos.GetType)
    x.Serialize(objStreamWriter, pos)
    objStreamWriter.Close()
     

    Now I want to add a Function (GenerateSendXML) to my class so that when I can make it more object oriented. In other words, call the function and create an XML file to be saved to a path provided or send it via a web service. So this is what I have so far.

    Public Function GenerateAndSendXML()
            Dim result As Boolean = False
            Dim objStreamWriter As New StreamWriter("C:\Orders.xml")
    
            Dim x As New XmlSerializer(this class)
    
            Try
                x.Serialize(objStreamWriter, this class)
                objStreamWriter.Close()
                result = True
            Catch ex As Exception
                Return result
            End Try
            
            Return result
        End Function

    My problem is where I want to provide the class itself to be serialized.

    Any ideas as to how to do it? In C# I believe I can use the word "this" and it will refer to itself. I am not sure in VB.NET.

    Thanks in advance.

    Thursday, January 2, 2014 3:50 PM
  • In VB the keyword to use is Me.  It is equivalent to 'this' in C#.

    Michael Taylor
    http://msmvps.com/blogs/p3net

    Thursday, January 2, 2014 4:39 PM
    Moderator
  • Michael,

    I know it's Me, but I was getting an error when using the "Me" in the class itself. So I was not sure if "Me" could be used within a class. I have used it in forms, which are really classes....of course. Somehow I was getting an error. Anyway, It's working like a charm.

    Thank you for you help.

    Thursday, January 2, 2014 5:07 PM