XML Class Deserialization from Class itself

  • Thread starter Thread starter Just D.
  • Start date Start date
J

Just D.

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.
 
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
 
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;
}

}
 
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;
}

}
 
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/<#[email protected]>

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. said:
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]
 
Jochen said:
Just D. wrote:




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.
 
Back
Top