dcsimg
Results 1 to 6 of 6

Thread: Serialize and deserialize structure data to-from XML

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2009
    Posts
    190

    Serialize and deserialize structure data to-from XML

    Hello,
    I need to serialize and deserialize data to-from XML.
    This seem's work OK.

    Code:
        Public Function structToXML(ByVal obj As Object) As String
    
            Dim x As New Xml.Serialization.XmlSerializer(obj.GetType())
            Dim sw As New StringWriter()
            x.Serialize(sw, obj)
            Return sw.ToString
        End Function
    So I can pack my structure data to XML string:
    Code:
    <Serializable()> _
    Public Structure myStruct
        Dim first As Integer
        Dim second As String
        Dim third As Double
    End Structure
    ...
    
            Dim p As New myStruct
            p.first = 1
            p.second = "abcde"
            p.third = 3.14
            Dim xmlstr As String = structToXML(p)
    With that I get valid XML string (xmlstr).

    Now I have function to fill structure data from same xmlstr which also look's good and passes without any reported errors but I don't know to call it properly or didn't make arguments properly:
    Code:
        Public Function XMLToStruct(ByVal xString As String, ByVal type As Type) As Object
    
            Dim x As New Xml.Serialization.XmlSerializer(type)
            Dim sw As New IO.StringReader(xString)
            Return x.Deserialize(sw)
        End Function
    I am try like that:
    Code:
    'Set new data to structure
    
            p.first = 2
            p.second = "fghi"
            p.third = 7.64
    
    'Load old data to structure myStruct, instance p
           p = XMLToStruct(xmlstr, GetType(myStruct))
    Here I get error: Option Strict On disallows implicit conversions from 'Object' to 'myproj.myStruct'.
    Of course, I woud like to keep Option Strict = On.

    What I am doing wrong and how to easiest to get data to p from xmlstr by using XMLToStruct function?
    Or it is needed to do some changes in function or what?

  2. #2
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148

    Re: Serialize and deserialize structure data to-from XML

    You have to explicitly convert the object retruend by the function XMLToStruct into a "myStruct" - use the CType() command.

    Code:
    p = CType(XMLToStruct(xmlstr, GetType(myStruct)), myStruct)

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2009
    Posts
    190

    Re: Serialize and deserialize structure data to-from XML

    Thanks Merrion, now function passes without error but I still don't get expected result.

    Code:
            Dim p As New myStruct
    
            p.first = 1
            p.second = "abcde"
            p.third = 3.14
    
            '' pack that to string
            Dim xmlstr As String = structToXML(p)
    
            '' change values
            p.first = 2
            p.second = "some new string"
            p.third = 7.64
    
            '' load packed data back in p
            p = CType(XMLToStruct(xmlstr, GetType(myStruct)), myStruct)
    After "load packed data back to p" I expect to have first values in p (1, abcde, 3.14).
    But I haven't, second values are in p.

    I try to change ByVal type as Type with ByRef type As Type in function XMLToStruct, also without result.

    Or I expect something wrongly?

    EDITED:
    Now I am pretty sure that my function XMLToStruct don't work at all.
    I would be glad if someone repair it.
    Last edited by nime; Jan 20th, 2014 at 09:43 AM.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Sep 2009
    Posts
    190

    Re: Serialize and deserialize structure data to-from XML

    No, my functions are OK.
    I finally find what is...

    That gives wrong result:
    Code:
         p = CType(XMLToStruct(xmlstr, GetType(myStruct)), myStruct)
    And that gives good result:
    Code:
         p = DirectCast(XMLToStruct(xmlstr, GetType(myStruct)), myStruct)
    How can that be?

  5. #5
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,242

    Re: Serialize and deserialize structure data to-from XML

    MSDN:

    DirectCast does not use the Visual Basic run-time helper routines for conversion, so it can provide somewhat better performance than CType when converting to and from data type Object.
    There you go.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Sep 2009
    Posts
    190

    Re: Serialize and deserialize structure data to-from XML

    Didn't know for that.
    For some time I don't use any VisualBasic namespaces at all.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width