将序列化后的信息存储成字符串,并从字符串中反序列化出来。
using System;
using System.IO;
using System.Text;
using System.Collections;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace WSerializable
{
/// <summary>
/// MySerialObject 的摘要说明。
/// </summary>
///
[Serializable]
public class MySerialObject
{
public MySerialObject()
{
}
private Hashtable hs = new Hashtable();
public Hashtable HT
{
get
{
return hs;
}
}
}
[Serializable]
public class MyObject
{
public MyObject()
{
x=y=height=width=0;
}
private int x;
private int y;
private int height;
private int width;
private string name;
public int X
{
get
{
return x;
}
set
{
x = value;
}
}
public int Y
{
get
{
return y;
}
set
{
y = value;
}
}
public int Height
{
get
{
return height;
}
set
{
height = value;
}
}
public int Width
{
get
{
return width;
}
set
{
width = value;
}
}
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
}
}
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Text;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml;
namespace WSerializable
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
int i=0;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
MySerialObject myobject;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(392, 360);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "序列化";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(488, 360);
this.button2.Name = "button2";
this.button2.TabIndex = 1;
this.button2.Text = "反序列化";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// label1
//
this.label1.Location = new System.Drawing.Point(104, 120);
this.label1.Name = "label1";
this.label1.TabIndex = 3;
this.label1.Text = "label1";
//
// label2
//
this.label2.Location = new System.Drawing.Point(312, 120);
this.label2.Name = "label2";
this.label2.TabIndex = 4;
this.label2.Text = "label2";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(680, 414);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
if(myobject == null)
myobject = new MySerialObject();
MyObject mo1 = new MyObject();
mo1.X = label1.Location.X;
mo1.Y = label1.Location.Y;
mo1.Height = label1.ClientSize.Height;
mo1.Width = label1.ClientSize.Width;
mo1.Name = label1.Name;
MyObject mo2 = new MyObject();
mo2.X = label2.Location.X;
mo2.Y = label2.Location.Y;
mo2.Height = label2.ClientSize.Height;
mo2.Width = label2.ClientSize.Width;
mo2.Name = label2.Name;
myobject.HT.Add("1",mo1);
myobject.HT.Add("2",mo2);
BinaryFormatter formatter = new BinaryFormatter();
MemoryStream memoryStream = new MemoryStream();
formatter.Serialize(memoryStream,myobject);
byte[] aryByte = memoryStream.ToArray();
string base64 = Convert.ToBase64String(aryByte,0,aryByte.Length);
memoryStream.Close();
XmlDocument xd = new XmlDocument();
xd.LoadXml("<?xml version=\"1.0\"?><project></project>");
XmlNode pd = (XmlNode)xd.DocumentElement;
XmlNode node = xd.CreateElement("base64");
node.InnerXml = base64;
pd.AppendChild(node);
xd.Save(@"D:\e.xml");
myobject = null;
}
private void Form1_Load(object sender, System.EventArgs e)
{
}
private void button3_Click(object sender, System.EventArgs e)
{
}
private void button2_Click(object sender, System.EventArgs e)
{
XmlDocument xd = new XmlDocument();
xd.Load(@"D:\e.xml");
string base64 = xd.DocumentElement.FirstChild.InnerXml;
byte[] aryByte = Convert.FromBase64String(base64);
BinaryFormatter formatter = new BinaryFormatter();
MemoryStream memoryStream = new MemoryStream(aryByte,0,aryByte.Length);
myobject = (MySerialObject)formatter.Deserialize(memoryStream);
memoryStream.Close();
MyObject mo1 = (MyObject)myobject.HT["1"];
MyObject mo2 = (MyObject)myobject.HT["2"];
label2.Top = mo1.X;
label2.Left = mo1.Y;
label2.Height = mo1.Height;
label2.Width = mo1.Width;
label1.Top = mo2.X;
label1.Left = mo2.Y;
label1.Height = mo2.Height;
label1.Width = mo2.Width;
myobject = null;
}
}
}