473,406 Members | 2,377 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,406 software developers and data experts.

Can a constructor deserialize to itself?

I would like an Object's Constructor to create itself by deserializing from XML. Is there an easy way to do this?

I could use a separate function to deserialize to an object but I'd like to do it in the Constructor. Here's an example of what I
mean;

Private Sub Page_Load()
' Sample XML:
' <?xml version=""1.0"" encoding=""UTF-8""?>
' <ObjectType>
' <SomeValue>Hello.</SomeValue>
' </ObjectType>

Dim NewObject As ObjectType = New ObjectType("<?xml version=""1.0""
encoding=""UTF-8""?><ObjectType><SomeValue>Hello.</SomeValue></ObjectType>")

End Sub

Public Class ObjectType
Public SomeValue As System.String

Public Sub New(ByVal xml As System.String)
Dim r As New System.IO.StringReader(xml)
Dim d As New System.XML.Serialization.XmlSerializer(GetType(Obj ectType))
'
' this works but is not very elegant since every property must be set manually.
'
Dim NewMe As New ObjectType
NewMe = d.Deserialize(r)
Me.SomeValue = NewMe.SomeValue

'
' this doesn't work but it's what I'm looking for.
'
Me = d.Deserialize(r)
End Sub

Public Sub New()
End Sub

End Class


Nov 20 '05 #1
1 1802
Stephen,
By the time the constructor runs the object's memory is already created, so
no the constructor cannot deserialize itself.

I would recommend you add a Shared method to the class that does the
deserialization for you..

Something like:
Dim NewObject As ObjectType = ObjectType.FromXml("<?xml version=""1.0"" encoding=""UTF-8""?><ObjectType><SomeValue>Hello.</SomeValue></ObjectType>")

Public Class ObjectType
Public SomeValue As System.String

Public Shared Sub FromXml(ByVal xml As System.String) As ObjectType Dim r As New System.IO.StringReader(xml)
Dim d As New System.XML.Serialization.XmlSerializer(GetType(Obj ectType))

I would then consider adding a "matching" "serialize" method to the class

Public Function ToXml() As String
Dim d as XmlSerializer(...)

Hope this helps
Jay

"Stephen Travis" <st*****@iname.com> wrote in message
news:O2**************@TK2MSFTNGP12.phx.gbl... I would like an Object's Constructor to create itself by deserializing from XML. Is there an easy way to do this?
I could use a separate function to deserialize to an object but I'd like to do it in the Constructor. Here's an example of what I mean;

Private Sub Page_Load()
' Sample XML:
' <?xml version=""1.0"" encoding=""UTF-8""?>
' <ObjectType>
' <SomeValue>Hello.</SomeValue>
' </ObjectType>

Dim NewObject As ObjectType = New ObjectType("<?xml version=""1.0"" encoding=""UTF-8""?><ObjectType><SomeValue>Hello.</SomeValue></ObjectType>")
End Sub

Public Class ObjectType
Public SomeValue As System.String

Public Sub New(ByVal xml As System.String)
Dim r As New System.IO.StringReader(xml)
Dim d As New System.XML.Serialization.XmlSerializer(GetType(Obj ectType)) '
' this works but is not very elegant since every property must be set manually. '
Dim NewMe As New ObjectType
NewMe = d.Deserialize(r)
Me.SomeValue = NewMe.SomeValue

'
' this doesn't work but it's what I'm looking for.
'
Me = d.Deserialize(r)
End Sub

Public Sub New()
End Sub

End Class

Nov 20 '05 #2

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

Similar topics

11
by: Amadrias | last post by:
Hi all, I am using a class to transport some data over the network. I then added the attribute to the class. My problem is that this class is part of a framework and that I do not want...
4
by: Bob Rock | last post by:
Hello, I've got an xml stream that I'd need to deserialize into an instance of a given class A. I'd like to create an instance method on class A (method Deserialize) that takes this XML stream...
7
by: Ian Tompsett | last post by:
H I was wondering if it possible for an object to serialize/deserialize itself from XML. I'd be guessing that it would need to use the XmlSerializer class, but that seems to want to create a...
0
by: Mark | last post by:
Hi all, i'm trying to serialize a class. Using the constructor of XmlSerializer i get these (odd?) errors: "File or assembly name goseij9w.dll, or one of its dependencies, was not found"....
3
by: Mark | last post by:
Hi all, i'm trying to serialize a class. Using the constructor of XmlSerializer i get these (odd?) errors: "File or assembly name goseij9w.dll, or one of its dependencies, was not found"....
2
by: MP | last post by:
Hello, I am having a bit of a problem with Deserialization. I need to "override" the special constructor used when an object is deserialize. In thes constructor I have to execute some code that...
5
by: Charles Law | last post by:
I have an object that is created like this Dim MyObj As MyClass MyObj = New MyClass(objInfo) The point of this being that the creation of the object requires some information so that it can...
2
by: jhcorey | last post by:
I have this method in class Foo: public Foo DeserializeXML(string sXML) { XmlSerializer serializer = new XmlSerializer(typeof(Foo)); StringReader sr = new StringReader(sXML); return...
2
by: punitsinghi | last post by:
Hi, I have a static generic dictionary in a class. As static memeber cannot serialized so i have implented ISerializable interface and method GetObjectData to serialize. I have a constructor which...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.