PC Review


Reply
Thread Tools Rate Thread

Deserialize to 'this'

 
 
Hoss
Guest
Posts: n/a
 
      23rd Jan 2007
Quick Remark.

I have a class that looks like this

[Serializable]
[XmlRoot("Class")]
public Class
{
[XmlAttribute]
public int Attribu
{
get{};set{};
}
[XmlAttribute]
public int Attribu
{
get{};set{};
}
}

Set up perfectly for Xml Serialization / Deserialization. One of the
great things about this is that if I, say, add a column to the
database, all I need to do is create the new public property on my
class. I do not have to say add a few lines to my SerializeToXml()
method, or add code to my DeserializeFromXml() method, since the
System.Xml.Serialization.XmlSerializer determines all that information
dynamically using Reflection. Okay, great, we have realized the
flexibility benefits from that.

But how do I set up my Deserialization constructor to work with that?
Consider:

public Class(string id)
{
string xml = FetchXml() // Get Xml representing this object
from somewhere. The ID is the key on the data.
Class instance = XmlSerializer.Deserialize(xml);
this.Attrib = instance.Attrib;
this.Attrib = instance.Attrib;
this.Attrib = instance.Attrib;
... ect.
}

You cant say
this = XmlSerializer.Deserialize(xml);

So - is there any way to Deserialize from xml and populate all the
properties in a CONSTRUCTOR without enumerating each property ?? I
guess I could use a static factory instead of a constructor.

Thoughts?

 
Reply With Quote
 
 
 
 
Samuel R. Neff
Guest
Posts: n/a
 
      23rd Jan 2007

Separate "this" and the object that's actually being serialized. Say
normally you have a class like this:

[XmlRoot("Class")]
public class C {

private int _att;

[XmlAttribute] public int Att { get { return _att; } set { _att =
value; } }

}

Then separate it into two classes:

public class C {
private C_Data _data;
public int Att { get { return _data.Att; } set { _data.Att = value;
} }
}

[XmlRoot("Class")]
public class C_Data {
[XmlAttribute] publi int Att;
}

The calling code still just sees class C all the same, but internally
you use C_Data for serialization/deserialization and data storage.

Unfortunately C_Data actually has to be public, so put it in a
different namespace so as not to confuse callers.

HTH,

Sam


------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.



On 22 Jan 2007 19:40:45 -0800, "Hoss" <(E-Mail Removed)> wrote:

>Quick Remark.
>
>I have a class that looks like this
>
>[Serializable]
>[XmlRoot("Class")]
>public Class
>{
> [XmlAttribute]
> public int Attribu
> {
> get{};set{};
> }
> [XmlAttribute]
> public int Attribu
> {
> get{};set{};
> }
>}
>
>Set up perfectly for Xml Serialization / Deserialization. One of the
>great things about this is that if I, say, add a column to the
>database, all I need to do is create the new public property on my
>class. I do not have to say add a few lines to my SerializeToXml()
>method, or add code to my DeserializeFromXml() method, since the
>System.Xml.Serialization.XmlSerializer determines all that information
>dynamically using Reflection. Okay, great, we have realized the
>flexibility benefits from that.
>
>But how do I set up my Deserialization constructor to work with that?
>Consider:
>
> public Class(string id)
> {
> string xml = FetchXml() // Get Xml representing this object
>from somewhere. The ID is the key on the data.
> Class instance = XmlSerializer.Deserialize(xml);
> this.Attrib = instance.Attrib;
> this.Attrib = instance.Attrib;
> this.Attrib = instance.Attrib;
> ... ect.
> }
>
>You cant say
>this = XmlSerializer.Deserialize(xml);
>
>So - is there any way to Deserialize from xml and populate all the
>properties in a CONSTRUCTOR without enumerating each property ?? I
>guess I could use a static factory instead of a constructor.
>
>Thoughts?


 
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
Re: Deserialize an XML Paul Microsoft C# .NET 0 3rd Nov 2009 04:04 PM
Re: Deserialize an XML Jani Järvinen [MVP] Microsoft ASP .NET 0 3rd Nov 2009 03:23 PM
Re: Deserialize an XML Family Tree Mike Microsoft C# .NET 0 2nd Nov 2009 10:02 PM
Deserialize XML where XML may contain XML nick_nw Microsoft C# .NET 1 5th Jul 2006 11:32 AM
XML deserialize Stream works ok but deserialize from XmlNodeReader fails Samuel R. Neff Microsoft VB .NET 4 8th Feb 2005 03:37 AM


Features
 

Advertising
 

Newsgroups
 


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