DeSerialization of an XML Stream

R

RSH

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
 
G

Guest

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 ;-)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top