Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > DeSerialization of an XML Stream

Reply
Thread Tools

DeSerialization of an XML Stream

 
 
RSH
Guest
Posts: n/a
 
      06-14-2007
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


 
Reply With Quote
 
 
 
 
=?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=
Guest
Posts: n/a
 
      06-14-2007
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
>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
XML Schema WSDL data type deserialization problem WideBoy XML 2 08-12-2006 08:46 PM
Deserialization Error--End of Stream =?Utf-8?B?U29sZWwgU29mdHdhcmU=?= ASP .Net 5 08-10-2004 12:17 PM
Deserialization Error--End Of Stream =?Utf-8?B?U29sZWwgU29mdHdhcmU=?= ASP .Net 1 07-30-2004 02:31 AM
Serialization/Deserialization or XML as DB umbertoeklat@yahoo.com ASP .Net 3 05-14-2004 03:47 PM



Advertisments