473,396 Members | 2,017 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

DeSerialization of an XML Stream

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
Jun 14 '07 #1
1 1224
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
Jun 14 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Tom L | last post by:
Simple deserialization help needed please... I have a packet of xml in a string, and need to get that into a reader./stream of some sort so I can properly use deserialize.. here's my...
2
by: Shone | last post by:
I would like to perform a 2-pass XML reading from a stream. Once using the Validating reader, just to confirm the validity against the schema, and next time to do a reading to extract the data....
3
by: Roy Chastain | last post by:
The following code fails with an exception of "There is an error in XML document (2, 2)." string with an inner exception of "Value cannot be null.\r\nParameter name: input" string. The inner...
3
by: Amadelle | last post by:
Hi all and thanks in advance for your help, I am having problems deserializing an object which seems to be serializing just fine. I save the byte array of the serialized object in the database...
1
by: Jim S | last post by:
I have an application where I have to make a tree of objects. To do this, I have my own node class. At certain points in the application, I need to save data. I am having a problem with the...
3
by: AnkitAsDeveloper [Ankit] | last post by:
Hi i am serializing a 'ref struct' object as follows : private: void Seri( String ^path, Object^ obj ) { FileStream^ fileStrm ; try { //Serialize entire object into Binary stream
2
by: andreas | last post by:
Hi, if have a object arrSdList of type SortedList for serialization and deserialization there are two subs for doing this Public Sub deser Dim Formatter As BinaryFormatter = New...
3
by: parrot toes | last post by:
Summary: I have been trying to make requests of a web service provided by Axis using a dotnet client with code generated by wsdl.exe and have been getting exceptions when trying to process the...
8
by: ashoksrini | last post by:
Hi All, I have the below requirement and would like to get some feeback from the group on the best way to implement: 1. I have WSDL defined exposing few web services. 2. We dont have a...
1
by: depalau | last post by:
I'm experiencing issues where XmlSerialier.Deserialize throws an exception when attempting to use a MemoryStream built with an XmlWriter vs. a standalone StringReader. It is attempting to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.