序列化存储在磁盘上和存储在数据库中的区别
1个回答
展开全部
public class Cat
{
public string Color { get; set; }
public int Speed { get; set; }
public string Name{ get; set; }
}
//序列化
var cat1=new Cat{Color="Write",Speed=50,Name="MiMi" };
XmlSerializer ser = new XmlSerializer(typeof(Cat));
MemoryStream ms = new MemoryStream();
ser.Serialize(ms, cat1);
string xmlString = Encoding.UTF8.GetString(ms.ToArray());
//xmlString要保存数据库字符串
//反序列化
XmlSerializer dser = new XmlSerializer(typeof(Cat));
//xmlString数据库获取字符串
Stream xmlStream = new MemoryStream(Encoding.UTF8.GetBytes(xmlString));
Cat cat2=dser.Deserialize(xmlStream) as Cat;//cat2 要class象
A
{
public string Color { get; set; }
public int Speed { get; set; }
public string Name{ get; set; }
}
//序列化
var cat1=new Cat{Color="Write",Speed=50,Name="MiMi" };
XmlSerializer ser = new XmlSerializer(typeof(Cat));
MemoryStream ms = new MemoryStream();
ser.Serialize(ms, cat1);
string xmlString = Encoding.UTF8.GetString(ms.ToArray());
//xmlString要保存数据库字符串
//反序列化
XmlSerializer dser = new XmlSerializer(typeof(Cat));
//xmlString数据库获取字符串
Stream xmlStream = new MemoryStream(Encoding.UTF8.GetBytes(xmlString));
Cat cat2=dser.Deserialize(xmlStream) as Cat;//cat2 要class象
A
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询