473,394 Members | 1,663 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,394 software developers and data experts.

Deserialize in constructor

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

Jun 12 '06 #1
2 5504

<jh*****@yahoo.com> wrote in message
news:11**********************@h76g2000cwa.googlegr oups.com...
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

Jun 12 '06 #2
<jh*****@yahoo.com> a écrit dans le message de news:
11**********************@h76g2000cwa.googlegroups. com...

|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
Jun 12 '06 #3

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

Similar topics

2
by: Greg | last post by:
I'm writing a class in C# .... I have a collection calls Reports made up of Report objects. I'm trying to deserialize an XML file that looks like : <Reports> <Report> <Title>some title</Title>...
4
by: Sebastien Tardif | last post by:
Subject: XmlSerializer.Deserialize complain when root declare the namespace If I do XmlSerializer.Deserialize( myString ) and myString is: String myString = "<?xml version=\"1.0\"...
0
by: JMD | last post by:
I have a set of schemas for two different XML files that share common structures through the use of "import". Eventually, the main XSD file (which just gathers pieces from the common stuff to...
0
by: Fruber Malcome | last post by:
I'm getting a very weird exception and hoping someone may be able to help. I have an Office Add-In that lives in a .dll (for email reference ai.dll) ai.dll makes calls into the core part of the...
4
by: George Addison | last post by:
I understand this might not be the optimal method of deserialization, but how can I deserialize a class to itself? Something like: Public Sub New(Optional ByVal filename as string = Nothing)...
1
by: Stephen Travis | last post by:
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...
6
by: Val | last post by:
How can I serialize/deserialize an object into a string. Existing examples seem to be showing this operation for files only. Thansk
2
by: Joe | last post by:
Hi I have a Generics List in a PropertyGrid I am able to Serialize it to XML but when I try to deserialize back to the class of the PropertyGrid The Constructor doesn't seem to fire to reload...
1
by: Hoss | last post by:
Quick Remark. I have a class that looks like this public Class { public int Attribu
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: 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:
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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.