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

XML Class Deserialization from Class itself

All,

What's the easiest way to deserialize class from the class itself? Step by
step. We create some class, add all required USINGs, make this class
serializable, then add a method like:

public string Serialize()
{
XmlSerializer xs = new XmlSerializer(typeof(CClassName));
MemoryStream ms = new MemoryStream();
TextWriter w = new StreamWriter(ms);
xs.Serialize(w, this);
xs=null;
w=null;
byte[] b = ms.ToArray();
ms.Close();
ms=null;
string s = Encoding.UTF8.GetString(b);
return s;
}

It works great, but now we need to deserialize. How can we create this
Deserialize method?

public int DeSerialize(string sXML)
{

What's here?

return 0;
}
We can;t create the deserialized class from the class itself, so what should
we do, create another instance of the object with serialization and then
assign all members one by one? I don't like this idea, but what else? Mabe
I'm missing something...

Just D.
Nov 16 '05 #1
7 4718
Hi,
I don't quite understand what the problem is. XmlSerializer does have a
Deserialize method that takes in a Stream and returns an Object. And of
course you can create an XmlSerializer the same way you created it in
Serialize method.

Regards
Ming Chen

"Just D." <no@spam.please> wrote in message
news:SQW4d.61136$9Y5.49642@fed1read02...
All,

What's the easiest way to deserialize class from the class itself? Step by
step. We create some class, add all required USINGs, make this class
serializable, then add a method like:

public string Serialize()
{
XmlSerializer xs = new XmlSerializer(typeof(CClassName));
MemoryStream ms = new MemoryStream();
TextWriter w = new StreamWriter(ms);
xs.Serialize(w, this);
xs=null;
w=null;
byte[] b = ms.ToArray();
ms.Close();
ms=null;
string s = Encoding.UTF8.GetString(b);
return s;
}

It works great, but now we need to deserialize. How can we create this
Deserialize method?

public int DeSerialize(string sXML)
{

What's here?

return 0;
}
We can;t create the deserialized class from the class itself, so what should we do, create another instance of the object with serialization and then
assign all members one by one? I don't like this idea, but what else? Mabe
I'm missing something...

Just D.

Nov 16 '05 #2
Hi,
I don't quite understand what the problem is. XmlSerializer does have a
Deserialize method that takes in a Stream and returns an Object. And of
course you can create an XmlSerializer the same way you created it in
Serialize method.


Thanks for the answer. The problem is that I need to describe all class
members if I want to deserialize from the same class. This code below works,
but I don't like it. Any other way?

Just D.

//-----------------------------------------------------------------------------------------
[Serializable()]
public class CSomeClass : CSomeClassBase
{
//-----------------------------------------------------------------------------------------
public CSomeClass() //Constructor
{
}

//-----------------------------------------------------------------------------------------
public string Serialize()
{
XmlSerializer xs = new XmlSerializer(typeof(CSomeClass));
MemoryStream ms = new MemoryStream();
TextWriter w = new StreamWriter(ms);
xs.Serialize(w, this);
xs=null;
w=null;
byte[] b = ms.ToArray();
ms.Close();
ms=null;
string s = Encoding.UTF8.GetString(b);
return s;
}

//-----------------------------------------------------------------------------------------
public int DeSerialize(string sXML)
{
byte[] ba = Encoding.ASCII.GetBytes(sXML);
MemoryStream ms = new MemoryStream(ba,0,ba.Length);
ms.Seek(0,0);
XmlSerializer ser = new XmlSerializer(typeof(CSomeClass));
CSomeClass ob = (CSomeClass)ser.Deserialize(ms);
this.Copy(ob);
ob = null;
ms.Close();
ms=null;
ser=null;
return 0;
}

//-----------------------------------------------------------------------------------------
public void Copy(CMedicalQuestionDerived o)
{
this.Member1 = o.Member1;
this.Member2 = o.Member2;
this.MemberN = o.MemberN;
}

}

Nov 16 '05 #3
Hi,
I don't quite understand what the problem is. XmlSerializer does have a
Deserialize method that takes in a Stream and returns an Object. And of
course you can create an XmlSerializer the same way you created it in
Serialize method.


Thanks for the answer. The problem is that I need to describe all class
members if I want to deserialize from the same class. This code below works,
but I don't like it. Any other way?

Just D.

//-----------------------------------------------------------------------------------------
[Serializable()]
public class CSomeClass : CSomeClassBase
{
//-----------------------------------------------------------------------------------------
public CSomeClass() //Constructor
{
}

//-----------------------------------------------------------------------------------------
public string Serialize()
{
XmlSerializer xs = new XmlSerializer(typeof(CSomeClass));
MemoryStream ms = new MemoryStream();
TextWriter w = new StreamWriter(ms);
xs.Serialize(w, this);
xs=null;
w=null;
byte[] b = ms.ToArray();
ms.Close();
ms=null;
string s = Encoding.UTF8.GetString(b);
return s;
}

//-----------------------------------------------------------------------------------------
public int DeSerialize(string sXML)
{
byte[] ba = Encoding.ASCII.GetBytes(sXML);
MemoryStream ms = new MemoryStream(ba,0,ba.Length);
ms.Seek(0,0);
XmlSerializer ser = new XmlSerializer(typeof(CSomeClass));
CSomeClass ob = (CSomeClass)ser.Deserialize(ms);
this.Copy(ob);
ob = null;
ms.Close();
ms=null;
ser=null;
return 0;
}

//-----------------------------------------------------------------------------------------
public void Copy(CSomeClass o)
{
this.Member1 = o.Member1;
this.Member2 = o.Member2;
this.MemberN = o.MemberN;
}

}
Nov 16 '05 #4
I think the problem is that you can't write

this = serializer.Deserialize();

why not use a static factory method which returns a deserialized object

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<#j**************@TK2MSFTNGP11.phx.gbl>

Hi,
I don't quite understand what the problem is. XmlSerializer does have a
Deserialize method that takes in a Stream and returns an Object. And of
course you can create an XmlSerializer the same way you created it in
Serialize method.

Regards
Ming Chen

"Just D." <no@spam.please> wrote in message
news:SQW4d.61136$9Y5.49642@fed1read02...
All,

What's the easiest way to deserialize class from the class itself? Step by
step. We create some class, add all required USINGs, make this class
serializable, then add a method like:

public string Serialize()
{
XmlSerializer xs = new XmlSerializer(typeof(CClassName));
MemoryStream ms = new MemoryStream();
TextWriter w = new StreamWriter(ms);
xs.Serialize(w, this);
xs=null;
w=null;
byte[] b = ms.ToArray();
ms.Close();
ms=null;
string s = Encoding.UTF8.GetString(b);
return s;
}

It works great, but now we need to deserialize. How can we create this
Deserialize method?

public int DeSerialize(string sXML)
{

What's here?

return 0;
}
We can;t create the deserialized class from the class itself, so what should we do, create another instance of the object with serialization and then
assign all members one by one? I don't like this idea, but what else? Mabe
I'm missing something...

Just D.


---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.766 / Virus Database: 513 - Release Date: 17/09/2004

[microsoft.public.dotnet.languages.csharp]
Nov 16 '05 #5
Hi Richard,
I think the problem is that you can't write
this = serializer.Deserialize();
Exactly! "this" is R/O.
why not use a static factory method which returns a deserialized object


What do you mean? Could you clarify?

Just D.
Nov 16 '05 #6
Just D. wrote:
Hi Richard,
I think the problem is that you can't write
this = serializer.Deserialize();
why not use a static factory method which returns a deserialized object


What do you mean? Could you clarify?


public static MyObject Deserialize(string s)
{
// ...
return (MyObject) serializer.Deserialize();
}

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Nov 16 '05 #7
Jochen Kalmbach wrote:
Just D. wrote:

Hi Richard,

I think the problem is that you can't write
this = serializer.Deserialize();
why not use a static factory method which returns a deserialized object


What do you mean? Could you clarify?

public static MyObject Deserialize(string s)
{
// ...
return (MyObject) serializer.Deserialize();
}


Before rolling your own, check out (and implement) the ISerializable
interface. It will open new doors for you. By using a built-in
interface, you'll immediately gain the benefits of all of the other
classes in the framework that utilize ISerializable. It'll give you
complete control over how your object will be serialized/deserialized.
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Nov 16 '05 #8

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

Similar topics

1
by: Pure Krome | last post by:
How do i serialize two instances (of some object) in the same class? I'm not sure how i would de-serialize a class that has two or more instances of some object type, and how the deserialize...
11
by: Amadrias | last post by:
Hi all, I am using a class to transport some data over the network. I then added the attribute to the class. My problem is that this class is part of a framework and that I do not want...
2
by: Just D | last post by:
Hi, I need to write a serialization (to XML string) and restoring (from XML string) of a very complicated object. The object uses a few classes, one class has two ArrayLists, etc. The general...
12
by: Michael Maes | last post by:
Hello, I have a BaseClass and many Classes which all inherit (directly) from the BaseClass. One of the functions in the BaseClass is to (de)serialize the (inherited) Class to/from disk. ...
0
by: bbalet.free.fr | last post by:
The “Add Web Reference” Visual tool generates bad classes (from WSDL schema) for ComplexType containing only one element (wsdl.exe and wseWsdl3.exe tools have the same problem) : if a...
0
by: bbalet.free.fr | last post by:
The “Add Web Reference” Visual tool generates bad classes (from WSDL schema) for ComplexType containing only one element (wsdl.exe and wseWsdl3.exe tools have the same problem) : if a...
4
by: jjkboswell | last post by:
I have an XSD which I have generated a class from using the xsd.exe tool. My XSD contains complex types within it, so that the generated class has member variables which are of types that are also...
3
by: Joe | last post by:
Hello all! I'm trying to deserialize an object which has a few members which no longer exist. Is there anyway using a custom binder to remove the reference to these objects or just return a...
3
by: Phill W. | last post by:
OK, I've asked nicely before; now I'm going to throw down the gauntlet to anyone brave enough to take it up. In VB'2005, can anyone write me a class that inherits from System.Data.DataTable, add...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
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...

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.