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

介绍了一种序列化和反序列化List<Entity>对象的方法,使用XML存储临时数据,避免直接操作数据库。适用于ASP.NET ViewState、Cookie、Cache等场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

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

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

None.gif    class Entity
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        
public Entity()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{}
InBlock.gif        
private int id;
InBlock.gif        
public int Id
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return id;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                id 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
private string name;
InBlock.gif        
public string Name
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return name;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                name 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private double price;
InBlock.gif        
public double Price
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return price;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                price 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

None.gif
None.gif

于是将他插入到List<Entity>对象中

None.gif    List<Entity> list = new List<Entity>();
None.gif    Entity obj 
= new Entity();
None.gif    obj.Id 
= 1;
None.gif    obj.Name 
= "test";
None.gif    obj.Price 
= 3.23;
None.gif    list.Add(obj);

 这样,一个List<Entity>对象就创建成功了,下面我们来将他序列化

None.gif        public static string Serialize<BusinessObject>(List<BusinessObject> GenericList)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            XmlDocument result 
= new XmlDocument();
InBlock.gif            result.LoadXml(
"<Root></Root>");
InBlock.gif            
foreach (BusinessObject obj in GenericList)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                XmlElement Item 
= result.CreateElement("Item");
InBlock.gif                PropertyInfo[] properties 
= obj.GetType().GetProperties();
InBlock.gif                
foreach (PropertyInfo property in properties)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if (property.GetValue(obj, null!= null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        XmlElement element 
= result.CreateElement(property.Name);
InBlock.gif                        element.SetAttribute(
"Type", property.PropertyType.Name);
InBlock.gif                        element.InnerText 
= property.GetValue(obj, null).ToString();
InBlock.gif                        Item.AppendChild(element);
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                result.DocumentElement.AppendChild(Item);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return result.InnerXml;
ExpandedBlockEnd.gif        }

 然后我们调用这个方法

None.gifstring str = Serialize<Entity>(list);

 生成的XML文件为:

None.gif    <Root>
None.gif        
<Item>
None.gif            
<Id Type="Int32">1</Id>
None.gif            
<Name Type="String">test</Name>
None.gif            
<Price Type="Double">3.23</Price>
None.gif        
</Item>
None.gif    
</Root>

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

None.gif        public static List<BusinessObject> Deserialize<BusinessObject>(string XmlStr)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            List
<BusinessObject> result = new List<BusinessObject>();
InBlock.gif            XmlDocument XmlDoc 
= new XmlDocument();
InBlock.gif            XmlDoc.LoadXml(XmlStr);
InBlock.gif            
foreach (XmlNode ItemNode in XmlDoc.GetElementsByTagName("Root").Item(0).ChildNodes)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                BusinessObject item 
= Activator.CreateInstance<BusinessObject>();
InBlock.gif                PropertyInfo[] properties 
= typeof(BusinessObject).GetProperties();
InBlock.gif                
foreach (XmlNode propertyNode in ItemNode.ChildNodes)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
string name = propertyNode.Name;
InBlock.gif                    
string type = propertyNode.Attributes["Type"].Value;
InBlock.gif                    
string value = propertyNode.InnerXml;
InBlock.gif                    
foreach (PropertyInfo property in properties)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
if (name == property.Name)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            property.SetValue(item,Convert.ChangeType(value,property.PropertyType), 
null);
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                result.Add(item);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return result;
ExpandedBlockEnd.gif        }

 然后我们调用这个方法: 

None.gifList<Entity> list = Deserialize<Entity>(str);

 完了。

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


转载于:https://www.cnblogs.com/kchen/archive/2006/11/04/550382.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值