您的位置:首页 > 其它

.NET 对象的序列化与反序列化 Binary,Soap,Xml

2012-10-31 22:53 483 查看
测试序列化的Model

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Runtime.Serialization;
using System.Xml.Serialization;

namespace SerializationDemo
{
[Serializable]
public class UserInfo
{
public UserInfo()
{

}

private string userName;

public string UserName
{
get { return userName; }
set { userName = value; }
}

[NonSerialized]
private string descrition;  //标记为NonSerialized的不会被序列化

public string Descrition
{
get { return descrition; }
set { descrition = value; }
}

/***
* 在序列化为xml时 标记为
* [XmlAnyAttribute]        成员将作为XML的特性被序列化
* [XmlElementAttribute]    字段或者特性将作为XML元素被序列化
* [XmlEnumAttribute]       枚举成员的元素名称
* [XmlRootAttribute]       该特性控制根元素如何被构造(命名空间和元素名称)
* [XmlTextAttribute]       属性或字段将被序列化为XML文本
* [XmlTypeAttribute]       XML类型的名称和命名空间
* */

[XmlAttribute]
private int age;    // 标记为XmlAttribute的将在序列化为xml时作为node的属性

public int Age
{
get { return age; }
set { age = value; }
}

private int sexy;

public int Sexy
{
get { return sexy; }
set { sexy = value; }
}

private bool hasBaby;

public bool HasBaby
{
get { return hasBaby; }
set { hasBaby = value; }
}

[OnSerializing]
private void OnSerializing(StreamingContext context)
{
//在序列化之时被调用的事件
}

[OnDeserialized]
private void OnDeserialized(StreamingContext context)
{
//在序列化完成后立即被调用的事件
}

[OnDeserializing]
private void OnDeserializing(StreamingContext context)
{
//在反序列化时被调用的方法
}

[OnDeserialized]
private void OnDeserialized(StreamingContext context)
{
//在反序列化后被立即调用的方法
}
}
}


序列化

private UserInfo m_User;

m_User = new UserInfo();
m_User.UserName = "张三";
m_User.Descrition = "测试序列化";
m_User.Sexy = 0;
m_User.Age = 15;
m_User.HasBaby = false;

private void SerializationObjectGraph(IFormatter itfFormat, Stream destStream, object graph)
{
itfFormat.Serialize(destStream, graph);
}

private void btnBinaryFormatter_Click(object sender, EventArgs e)
{
try
{
BinaryFormatter binFormat = new BinaryFormatter();
using (Stream fStream = new FileStream("User.dat", FileMode.Create, FileAccess.ReadWrite, FileShare.None))
{
SerializationObjectGraph(binFormat, fStream, m_User);
MessageBox.Show("Success!");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

private void btnSoapFormater_Click(object sender, EventArgs e)
{
try
{
SoapFormatter soapFormat = new SoapFormatter();
using (Stream fStream = new FileStream("User.dat", FileMode.Create, FileAccess.ReadWrite, FileShare.None))
{
SerializationObjectGraph(soapFormat, fStream, m_User);
MessageBox.Show("Success!");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

private void btnXmlFormater_Click(object sender, EventArgs e)
{
try
{
XmlSerializer xmlFormat = new XmlSerializer(typeof(UserInfo));
using (Stream fStream = new FileStream("User.dat", FileMode.Create, FileAccess.ReadWrite, FileShare.None))
{
xmlFormat.Serialize(fStream, m_User);
MessageBox.Show("Success!");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: