serialize.cs
来自「C#中对象的串序化示例,有多种方法可以实现,但是有各自的优缺点」· CS 代码 · 共 66 行
CS
66 行
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Collections;
namespace serialization
{
/// <summary>
/// Summary description for Serialize.
/// </summary>
public class Serialize
{
public Serialize()
{
//
// TODO: Add constructor logic here
//
}
public string EnSerialize(object Pd)
{
MemoryStream ms = new MemoryStream();
XmlSerializer sr=new XmlSerializer(Pd.GetType());
sr.Serialize(ms,Pd);
ms.Close();
byte [] bt = ms.GetBuffer();
string str=null;
for(int i=0;i<bt.Length;i++)
{
str+=Convert.ToChar(bt[i]);
}
return str;
}
public object DeSerialize(string str)
{
char [] ch = str.ToCharArray();
byte [] bt = new byte[str.Length];
for(int i=0;i<ch.Length;i++)
{
bt[i] = (byte)ch[i];
}
MemoryStream ms = new MemoryStream(bt,0,bt.Length);
XmlSerializer newSr=new XmlSerializer(typeof(System.Type));
object newPd = null;
newPd=(object)newSr.Deserialize(ms);
return newPd;
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?