Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / June 2007

Tip: Looking for answers? Try searching our database.

DeSerialization of an XML Stream

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
RSH - 14 Jun 2007 22:23 GMT
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
Milosz Skalecki [MCAD] - 14 Jun 2007 23:37 GMT
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 ;-)
Signature

Milosz

> Hi,
>
[quoted text clipped - 50 lines]
>
> Ron

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.