Results 1 to 8 of 8

Thread: [RESOLVED] Xml Serializer

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2009
    Location
    sydney
    Posts
    263

    Resolved [RESOLVED] Xml Serializer

    Hi Guys,
    I have an Object BomCondition, which i serialize to XML allowing users to edit values and then save into SQL in XML format.
    i get an error when try to convert string to object, here is my code and not sure what im doing wrong. help please

    Serializing works fine
    Code:
     Public Shared Function SerializeCondition(Con As BomCondition) As String
    
            'Dim StrWriter As New IO.StringWriter ' cannot be used as it encodes in UTF-16 
    
            Dim _StreamWriter As System.IO.StreamWriter = New IO.StreamWriter("BCXML.xml") ', System.Text.Encoding.UTF8)
    
            Dim Writer As Serialization.XmlSerializer = New Serialization.XmlSerializer(Con.GetType)
    
            Writer.Serialize(_StreamWriter, Con) ' should encode in utf-8 
            _StreamWriter.Close()
    
            ' Now read the file into a string {work around}
            Dim SR As New System.IO.StreamReader("BCXML.xml")
            Dim xml As String = SR.ReadToEnd()
            SR.Close()
            Return xml
    
        End Function
    Serialize error : unable to cast object of type string to BomCondition

    Code:
    Public Shared Function DeserialiseCondition(Con As String) As BomCondition
       
            Dim SW As System.IO.StreamWriter = New IO.StreamWriter("BCXML.xml")
            SW.Write(Con)
            SW.Close()
    
            Dim xmlRoot As XmlRootAttribute = New XmlRootAttribute With {.ElementName = "BomCondition", .IsNullable = True}
    
            Dim xmlreader As Serialization.XmlSerializer = New Serialization.XmlSerializer(Con.GetType, xmlRoot)
            Dim FS As New FileStream("BCXML.xml", FileMode.Open)
            Dim BC As BomCondition = CType(xmlreader.Deserialize(FS), BomCondition) 'New System.IO.StringReader(Con)
            FS.Close()
    
            Return BC
    
        End Function
    the reason i use stream writer rather than a string stream is a work around to get the UTF-8 encoding

    Thanks in advance
    Last edited by wiss.dev; Dec 19th, 2016 at 06:53 PM.

  2. #2
    Frenzied Member PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Newport, UK
    Posts
    1,484

    Re: Xml Serializer

    Both of those code snippets are the same DeserialiseCondition function, is that what you meant to post?

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2009
    Location
    sydney
    Posts
    263

    Re: Xml Serializer

    my bad, fixed

  4. #4
    Frenzied Member PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Newport, UK
    Posts
    1,484

    Re: Xml Serializer

    Not sure if it will help but Jon Skeet has an example of creating a StringWriter than can do UTF-8 over at http://stackoverflow.com/questions/3...utf-8-encoding - the code is C# but converting it to VB should be hard, it is only three lines of code.

    That will eliminate the need to write files and read them back in, potentially eliminating one source of problems.

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    33,489

    Re: Xml Serializer

    Why are you writing to a file in the deserializer?
    Code:
            Dim SW As System.IO.StreamWriter = New IO.StreamWriter("BCXML.xml")
            SW.Write(Con)
            SW.Close()
    Doesn't make sense to me... serializing writes the data out... deserializing should be reading it back in... If it's because you already have it in XML format in the string, then simply use a MemoryStream to feed it back to the deserializer and skip the file.

    -tg

    edit - I see now that your serializer reads the file back in... whaaaa?
    * 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??? *

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2009
    Location
    sydney
    Posts
    263

    Re: Xml Serializer

    [QUOTE=techgnome;5120627]Why are you writing to a file in the deserializer?

    This was a work around to get UTF-8 encoding. Memory stream encodes xml with UTF-16 and its a read only property.

    Issue thogh is the error on calling Deserialize method.
    Cannot cast string to object Bomcondition
    ......................................

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2009
    Location
    sydney
    Posts
    263

    Re: Xml Serializer

    ....

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2009
    Location
    sydney
    Posts
    263

    Re: Xml Serializer

    Dim xmlreader As Serialization.XmlSerializer = New Serialization.XmlSerializer(Con.GetType, xmlRoot)

    problem, i accidently passed the worng argument to serialize, Con is a string type, and i should pass a Bomcondition type.

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