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

Deserialize to 'this'

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?

Jan 23 '07 #1
1 1859

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" <to********@gmail.comwrote:
>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?
Jan 23 '07 #2

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

Similar topics

1
by: Carl Mercier | last post by:
Hi! I have 2 different applications/assembly. The first one creates an object and serializes it to a textfile on disk. The second one has the the exact same class (copied/pasted). It reads...
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...
3
by: Mullin Yu | last post by:
I want to know how can i deserialize an object to a string, instead of a file as below:ShoppingList myList = new ShoppingList(); myList.AddItem( new Item( "eggs",1.49 ) ); myList.AddItem( new...
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>...
2
by: PCH | last post by:
I have 2 functions, one to serialize an object, and one to deserialize it. I can serialize just fine, the problem is when I try to deserialize it later... I get an error: {"Invalid...
4
by: Samuel R. Neff | last post by:
I'm deserializing an XML file. If I pass a Stream to the file directly to the deserializer as follows it works fine: o = (New XmlSerializer( GetType(...
2
by: Thomas S | last post by:
Any suggestions on how to deserialize an object from one line of XML? I'm trying to deserialize multiple objects from one XML document, each object on one line of the file. The serialization is...
4
by: Anbu | last post by:
Hi All, I need to Deserialize the SoapService's SoapEnvelope response object. Here is the Body of the SoapEnvelope received as respone, <q1:serviceResponse...
1
by: Heron | last post by:
Hi, I'm having a problem deserializing my streams since they are utf8 encoded (they are being received over tcp/ip) so I was looking for a way to make the serializer use utf8, is there any? ...
0
by: connectpalm03-forum | last post by:
I have a class named (MyClassA) in ControlClasses.dll and was able to serialize it to database. Like below SaveTo(MemoryStream stream) { IFormatter formatter = new BinaryFormatter(); ...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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...

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.