Re: Serializing/Deserializing to Database
- From: stcheng@xxxxxxxxxxxxxxxxxxxx (Steven Cheng[MSFT])
- Date: Wed, 31 May 2006 03:56:35 GMT
Hi Alex,
For serializing and derializing .net class object so as to persist them as
text in database, you can consider both XmlSerialization(pure xml string
based) or BinarySerialization(need to convert the serialized binary data
into string, such as base64 convertion). Here are some simple code snippet
demostrating on using both binary and XML serialization to persist and
restore the class instance:
=========binary serialize============
//serialize the class instance and store the result into a TextBox
private void btnSerialize_Click(object sender, EventArgs e)
{
MySavableClass msc = new MySavableClass();
msc.MyID = "id1";
msc.Title = "default title";
msc.Length = 100;
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bf.Serialize(ms, msc);
byte[] bytes = ms.ToArray();
string base64data = Convert.ToBase64String(bytes);
textBox1.Text = base64data;
ms.Close();
}
//Deserialize the class instance from the text in the textbox
private void btnDeserialize_Click(object sender, EventArgs e)
{
byte[] bytes = Convert.FromBase64String(textBox1.Text);
MemoryStream ms = new MemoryStream(bytes);
ms.Position = 0;
BinaryFormatter bf = new BinaryFormatter();
object obj = bf.Deserialize(ms);
ms.Close();
MySavableClass msc = obj as MySavableClass;
if (msc != null)
{
MessageBox.Show(
string.Format("MyID: {0}\r\nTitle: {1}\r\nLength: {2}",
msc.MyID,msc.Title,msc.Length)
);
}
}
=============Xml serialization===============
private void btnXmlSerialize_Click(object sender, EventArgs e)
{
MySavableClass msc = new MySavableClass();
msc.MyID = "id1";
msc.Title = "default title";
msc.Length = 100;
XmlSerializer serializer = new
XmlSerializer(typeof(MySavableClass));
StringWriter sw = new StringWriter();
serializer.Serialize(sw, msc);
textBox1.Text = sw.ToString();
sw.Close();
}
private void btnXmlDeserialize_Click(object sender, EventArgs e)
{
XmlSerializer serializer = new
XmlSerializer(typeof(MySavableClass));
StringReader sr = new StringReader(textBox1.Text);
object obj = serializer.Deserialize(sr);
sr.Close();
MySavableClass msc = obj as MySavableClass;
if (msc != null)
{
MessageBox.Show(
string.Format("MyID: {0}\r\nTitle: {1}\r\nLength: {2}",
msc.MyID, msc.Title, msc.Length)
);
}
}
=======================================
=======test class==========
[Serializable]
public class MySavableClass
{
private string _myid;
private string _title;
private int _length;
public MySavableClass()
{
}
public string MyID
{
get { return _myid; }
set { _myid = value; }
}
public string Title
{
get { return _title; }
set { _title = value; }
}
public int Length
{
get { return _length; }
set { _length = value; }
}
}
========================
In addition, you can have a look at the MSDN document about .net
serialization for further reference:
#XML and SOAP Serialization
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconserialization.asp?
frame=true
#Binary Serialization
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconbinaryserializatio
n.asp?frame=true
Hope this helps.
Regards,
Steven Cheng
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
.
- References:
- Re: Serializing/Deserializing to Database
- From: Greg Young
- Re: Serializing/Deserializing to Database
- From: Alex Maghen
- Re: Serializing/Deserializing to Database
- From: Alex Maghen
- Re: Serializing/Deserializing to Database
- From: Greg Young
- Re: Serializing/Deserializing to Database
- From: Alex Maghen
- Re: Serializing/Deserializing to Database
- Prev by Date: FrameWork 2.0 deployment as part of Deployment project
- Next by Date: Re: How to change Company Name in Vistual studio 2005
- Previous by thread: Re: Serializing/Deserializing to Database
- Next by thread: ToolTip on WebBrowser and RichTextBox controls
- Index(es):
Relevant Pages
|