PC Review


Reply
Thread Tools Rate Thread

Can a constructor deserialize to itself?

 
 
Stephen Travis
Guest
Posts: n/a
 
      14th Jul 2004
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(ObjectType))
'
' 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




 
Reply With Quote
 
 
 
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      14th Jul 2004
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(ObjectType))

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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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(ObjectType))
> '
> ' 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
>
>
>
>



 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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
Deserialize in constructor jhcorey@yahoo.com Microsoft C# .NET 2 12th Jun 2006 09:17 PM
The constructor to deserialize an object of type '...' was not found Dan Holmes Microsoft C# .NET 1 24th Feb 2006 10:07 PM
XML deserialize Stream works ok but deserialize from XmlNodeReader fails Samuel R. Neff Microsoft VB .NET 4 8th Feb 2005 03:37 AM
Calling a struct constructor in a class constructor body Karl M Microsoft VC .NET 4 19th Dec 2004 01:21 PM
Deserialize Only Calls Default Constructor Charles Law Microsoft VB .NET 5 18th Aug 2004 10:57 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:30 PM.