PC Review


Reply
Thread Tools Rate Thread

Deserialize in constructor

 
 
jhcorey@yahoo.com
Guest
Posts: n/a
 
      12th Jun 2006
I have this method in class Foo:

public Foo DeserializeXML(string sXML)
{
XmlSerializer serializer = new XmlSerializer(typeof(Foo));
StringReader sr = new StringReader(sXML);
return (Foo)serializer.Deserialize(sr);

}

which seems to work fine if I do this:
Foo myFoo = new Foo();
myFoo = myFoo.Deserialize(myXMLString);

I'm trying to figure out if there's a simple way to do the
deserialization in a constructor.

TIA,
Jim

 
Reply With Quote
 
 
 
 
Joanna Carter [TeamB]
Guest
Posts: n/a
 
      12th Jun 2006
<(E-Mail Removed)> a écrit dans le message de news:
(E-Mail Removed)...

|I have this method in class Foo:
|
| public Foo DeserializeXML(string sXML)
| {
| XmlSerializer serializer = new XmlSerializer(typeof(Foo));
| StringReader sr = new StringReader(sXML);
| return (Foo)serializer.Deserialize(sr);
|
| }
|
| which seems to work fine if I do this:
| Foo myFoo = new Foo();
| myFoo = myFoo.Deserialize(myXMLString);
|
| I'm trying to figure out if there's a simple way to do the
| deserialization in a constructor.

public class Foo
{
// private fields

public Foo(string sXML) : base ()
{
Deserialize(sXML);
}

private void DeserializeXML(string sXML)
{
// assign decoded XML to private fields
}
}

{
Foo myFoo = new Foo(myXMLString);
...
}

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer


 
Reply With Quote
 
David Browne
Guest
Posts: n/a
 
      12th Jun 2006

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I have this method in class Foo:
>
> public Foo DeserializeXML(string sXML)
> {
> XmlSerializer serializer = new XmlSerializer(typeof(Foo));
> StringReader sr = new StringReader(sXML);
> return (Foo)serializer.Deserialize(sr);
>
> }
>
> which seems to work fine if I do this:
> Foo myFoo = new Foo();
> myFoo = myFoo.Deserialize(myXMLString);
>
> I'm trying to figure out if there's a simple way to do the
> deserialization in a constructor.
>


Make DeserializeXML static. Then you don't need an instance to call it.

public static Foo DeserializeXML(string sXML)
{

.. . .


Foo myFoo = Foo.Deserialize(myXMLString);


David



 
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
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
Can a constructor deserialize to itself? Stephen Travis Microsoft VB .NET 1 14th Jul 2004 06:38 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:38 PM.