All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
通过ADO.NET访问数据库,教程与实例_[Asp.Net教程]
精通数据库系列之入门-基础篇3_mssql学习_编程技术
ASP.NET查询ACCESS数据库的内容并在DATAVIEW中显示出来_.net资料_编程技术
专家讲解用.NET编写串口程序的一点心得_.net资料_编程技术
ASP.NET技巧:存储过程的分析_[Asp.Net教程]
为什么PHP令人不爽(对于大型系统)_php资料_编程技术
MIS开发中.net,Framework的打印功能_[Asp.Net教程]
php常用数据库备份类_[PHP教程]
在ASP.NET中自动给URL加上超级链接_[Asp.Net教程]
XML入门教程:掌握学习 XML 语法规则_[XML教程]
Reading number is top 10 pictures
Look for from human art net, is good1
Kim jong il's mistress, national beauty JinYuJi actor3
Sexy women in 2013--2
Go to the national museum1
Sell the barbecue as says father du breul5
囚犯暴乱了咋办?
沙漠里的美女
Tie a large font of mouse
Sanya, hainan Haitian party feast promiscuity
Group of female porn in 《westwards》, uninhibited woman threatened to not the bottom line2
Download software ranking
中国结婚习俗实录
Boxer vs Yellow4
Ashlynn Video2
打鸟视频
双旗镇刀客A
Macromedia Dreamweaver 8
Eclipse-CALMSANNY (second edition)
Jinling thirteen stock
Tram sex maniac 2 (H) rar bag6
天龙八部最新服务端
aaa published in(发表于) 2013/12/11 8:31:27 Edit(编辑)
通过序列化和反序列化泛型数据实体集合来实现持久化数据对象的方法_.net资料_编程技术

通过序列化和反序列化泛型数据实体集合来实现持久化数据对象的方法_.net资料_编程技术

通过序列化和反序列化泛型数据实体集合来实现持久化数据对象的方法_.net资料_编程技术-你的首页-uuhomepage.com

通过序列化和反序列化泛型数据实体集合来实现持久化数据对象的方法


我们在平时使用数据库的时候,经常会碰到一个问题,就是不希望数据实体对象插入数据库中, 却有想持久化的时候,那么就可以用序列化成


XML字符串,来保存到其他地方,由于生成的是字符串,所以可以保存到任意我们想保存的地方。比如 asp.net的ViewState,cookie,cache等。


首先,我们定义一个数据实体类。



class Entity
{
public Entity()
{}
private int id;
public int Id
{
get
{
return id;
}
set
{
id = value;
}
}
private string name;
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}


private double price;
public double Price
{
get
{
return price;
}
set
{
price = value;
}
}
}



于是将他插入到List对象中


List list = new List();
Entity obj = new Entity();
obj.Id = 1;
obj.Name = "test";
obj.Price = 3.23;
list.Add(obj);
这样,一个List对象就创建成功了,下面我们来将他序列化


public static string Serialize(List GenericList)
{
XmlDocument result = new XmlDocument();
result.LoadXml("");
foreach (BusinessObject obj in GenericList)
{
XmlElement Item = result.CreateElement("Item");
PropertyInfo[] properties = obj.GetType().GetProperties();
foreach (PropertyInfo property in properties)
{
if (property.GetValue(obj, null) != null)
{
XmlElement element = result.CreateElement(property.Name);
element.SetAttribute("Type", property.PropertyType.Name);
element.InnerText = property.GetValue(obj, null).ToString();
Item.AppendChild(element);
}
}
result.DocumentElement.AppendChild(Item);
}
return result.InnerXml;
}
然后我们调用这个方法


string str = Serialize(list);
生成的XML文件为:




1
test
3.23


下面,我们根据上面生成的xml文件,将他反序列化,生成刚才的List对象


public static List Deserialize(string XmlStr)
{
List result = new List();
XmlDocument XmlDoc = new XmlDocument();
XmlDoc.LoadXml(XmlStr);
foreach (XmlNode ItemNode in XmlDoc.GetElementsByTagName("Root").Item(0).ChildNodes)
{
BusinessObject item = Activator.CreateInstance();
PropertyInfo[] properties = typeof(BusinessObject).GetProperties();
foreach (XmlNode propertyNode in ItemNode.ChildNodes)
{
string name = propertyNode.Name;
string type = propertyNode.Attributes["Type"].Value;
string value = propertyNode.InnerXml;
foreach (PropertyInfo property in properties)
{
if (name == property.Name)
{
property.SetValue(item,Convert.ChangeType(value,property.PropertyType), null);
}
}
}
result.Add(item);
}
return result;
}
然后我们调用这个方法:


List list = Deserialize(str);
完了。


本文只是给大家介绍了序列化List<>对象的简单方法,用的时候要根据自己的情况而定。






添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.