DeSerialization of an XML Stream

Discussion in 'ASP .Net' started by RSH, Jun 14, 2007.

  1. RSH

    RSH Guest

    Hi,

    I have a situation where I am serializing a custom object into an XML string
    that is stored on a Session variable.

    The problem is I can't seem to figure out how to get it back to an object.

    Serialization:
    ' WORKS FINE

    Public Function SerializeObject(ByVal obj As CompanyBase)

    Dim oXS As XmlSerializer = New XmlSerializer(obj.GetType)

    Dim oStrW As New StringWriter

    Dim sXML As String

    oXS.Serialize(oStrW, obj)

    sXML = oStrW.ToString()

    oStrW.Close()

    Return sXML

    End Function



    ' GENERATES AN 'ILLEGAL CHARACTERS EXCEPTION

    ' str is the XML string created by the function above

    Public Function DeserializeObject(ByVal str As String) As CompanyBase

    Dim oCompanyBase As CompanyBase = New CompanyBase

    Dim oXS As XmlSerializer = New XmlSerializer(oCompanyBase.GetType) <-------
    Is there a way to not have to instantiate oCompany just to get at the type?

    Dim oStmR As StreamReader

    oStmR = New StreamReader(str)

    oCompanyBase = CType(oXS.Deserialize(oStmR), CompanyBase)

    oStmR.Close()

    Return oCompanyBase

    End Function



    Thanks!

    Ron
    RSH, Jun 14, 2007
    #1
    1. Advertising

  2. Hi there,

    In this case i would implement ISerializable interface within the
    CompantBase class:

    -- begin CompanyBase.vb --

    Imports System.Runtime.Serialization

    Public Class CompanyBase
    Implements ISerializable

    Public Sub New()

    End Sub

    Private Sub New(ByVal info As SerializationInfo, ByVal context As
    StreamingContext)
    Me._companyName = info.GetString("CompanyName")
    Me._employeeCount = info.GetInt32("EmployeeCount")
    End Sub

    Public Sub GetObjectData( _
    ByVal info As SerializationInfo, _
    ByVal context As StreamingContext) _
    Implements ISerializable.GetObjectData
    info.AddValue("CompanyName", Me.CompanyName)
    info.AddValue("EmployeeCount", Me.EmployeeCount)
    End Sub

    Private _companyName As String
    Public Property CompanyName() As String
    Get
    Return _companyName
    End Get
    Set(ByVal value As String)
    _companyName = value
    End Set
    End Property

    Private _employeeCount As Integer
    Public Property EmployeeCount() As Integer
    Get
    Return _employeeCount
    End Get
    Set(ByVal value As Integer)
    _employeeCount = value
    End Set
    End Property

    End Class

    -- end CompanyBase.vb --


    or simply mark your class with Serializable attribute:
    <Serializable()> _
    Public Class CompanyBase
    ....
    end class

    and then de/serialize like this

    -- begin default.aspx --

    Protected Sub Page_Load(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles Me.Load



    Dim xml As String
    Dim company As CompanyBase
    Dim serializer As New _
    System.Xml.Serialization.XmlSerializer( _
    GetType(CompanyBase), _
    "http://companyBase")

    If IsPostBack Then

    xml = Session("companyData")

    Using reader As New System.IO.StringReader(xml)
    company = CType(serializer.Deserialize(reader), _
    CompanyBase)
    End Using

    Else

    company = New CompanyBase()
    company.CompanyName = "Microsoft"
    company.EmployeeCount = 1200

    Using writter As New System.IO.StringWriter()
    serializer.Serialize(writter, company)
    writter.Flush()
    xml = writter.ToString()
    End Using

    Session("companyData") = xml

    End If

    end sub
    -- end default.aspx --



    Hope this helps
    vb.net sucks ;-)
    --
    Milosz


    "RSH" wrote:

    > Hi,
    >
    > I have a situation where I am serializing a custom object into an XML string
    > that is stored on a Session variable.
    >
    > The problem is I can't seem to figure out how to get it back to an object.
    >
    > Serialization:
    > ' WORKS FINE
    >
    > Public Function SerializeObject(ByVal obj As CompanyBase)
    >
    > Dim oXS As XmlSerializer = New XmlSerializer(obj.GetType)
    >
    > Dim oStrW As New StringWriter
    >
    > Dim sXML As String
    >
    > oXS.Serialize(oStrW, obj)
    >
    > sXML = oStrW.ToString()
    >
    > oStrW.Close()
    >
    > Return sXML
    >
    > End Function
    >
    >
    >
    > ' GENERATES AN 'ILLEGAL CHARACTERS EXCEPTION
    >
    > ' str is the XML string created by the function above
    >
    > Public Function DeserializeObject(ByVal str As String) As CompanyBase
    >
    > Dim oCompanyBase As CompanyBase = New CompanyBase
    >
    > Dim oXS As XmlSerializer = New XmlSerializer(oCompanyBase.GetType) <-------
    > Is there a way to not have to instantiate oCompany just to get at the type?
    >
    > Dim oStmR As StreamReader
    >
    > oStmR = New StreamReader(str)
    >
    > oCompanyBase = CType(oXS.Deserialize(oStmR), CompanyBase)
    >
    > oStmR.Close()
    >
    > Return oCompanyBase
    >
    > End Function
    >
    >
    >
    > Thanks!
    >
    > Ron
    >
    >
    >
    =?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=, Jun 14, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    627
    Sahil Malik
    May 14, 2004
  2. =?Utf-8?B?U29sZWwgU29mdHdhcmU=?=

    Deserialization Error--End Of Stream

    =?Utf-8?B?U29sZWwgU29mdHdhcmU=?=, Jul 29, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    421
    Steven Cheng[MSFT]
    Jul 30, 2004
  3. =?Utf-8?B?U29sZWwgU29mdHdhcmU=?=

    Deserialization Error--End of Stream

    =?Utf-8?B?U29sZWwgU29mdHdhcmU=?=, Jul 29, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    3,371
    Marcin
    Aug 10, 2004
  4. WideBoy
    Replies:
    2
    Views:
    816
    WideBoy
    Aug 12, 2006
  5. babaxter1
    Replies:
    0
    Views:
    1,791
    babaxter1
    Nov 14, 2006
Loading...

Share This Page