Re: Serializing/Deserializing to Database



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.)




.



Relevant Pages

  • Re: Current JSON Proposal in ES4
    ... String.parseJSON <-- should return what, ... Object should not always have data structure functionality. ... also supports a specialized serialization. ... in the face of a growing use of JSON as a data exchange ...
    (comp.lang.javascript)
  • Re: XmlSerializer Collection with Collections
    ... What you can do here is hide the OptionList from serialization as follows: ... > public class TestSerializer ... > public int AddQuestion(Question question) ... > public Question(string QuestionText, string Type, int Score, bool ...
    (microsoft.public.dotnet.xml)
  • Re: Compiler executable file c:..v1.1.4322csc.exe cannot be found.
    ... most likely you are using default serialization code and impersonation. ... But as the compiler _IS_ there, I don't see why it does this ... options, String compilerDirectory, String compilerExe, String ... CompilerParameters parameters, Evidence evidence) +478 ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Using Classes as List Items
    ... | serialization has three items for that property, ... | Private _Part1 As String ... | Public Property Part1() As String ...
    (microsoft.public.dotnet.languages.vb)