Objects | collections | data
Methods to implement persistent data objects by serializing and deserializing a generic data entity collection
When we use the database, we often encounter a problem, that is, do not want to insert the data entity objects into the database, but there is a need to persist, then you can use the serialization of
XML string, to be saved elsewhere, because it generates a string, so it can be saved to any place we want to save. such as ASP.net's viewstate,cookie,cache and so on.
First, we define a data entity class.
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;
}
}
}
and inserted him into the List<entity> object.
list<entity> list = new list<entity> ();
Entity obj = new Entity ();
Obj. Id = 1;
Obj. Name = "Test";
Obj. Price = 3.23;
List. ADD (obj);
In this way, a List<entity> object is created, and we're going to serialize him here.
public static string serialize<businessobject> (List<businessobject> genericlist)
{
XmlDocument result = new XmlDocument ();
Result. Loadxml ("<Root></Root>");
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;
}
And then we call this method
String str = serialize<entity> (list);
The generated XML file is:
<Root>
<Item>
<id type= "Int32" >1</Id>
<name type= "String" >test</Name>
<price type= "Double" >3.23</Price>
</Item>
</Root>
Next, we will deserialize the XML file generated above to generate the List<entity> object
public static list<businessobject> deserialize<businessobject> (String xmlstr)
{
list<businessobject> result = new list<businessobject> ();
XmlDocument xmldoc = new XmlDocument ();
Xmldoc.loadxml (XMLSTR);
foreach (XmlNode itemnode in Xmldoc.getelementsbytagname ("Root"). Item (0). ChildNodes)
{
Businessobject item = activator.createinstance<businessobject> ();
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;
}
And then we call this method:
list<entity> list = deserialize<entity> (str);
Finished.
This article is just for you to introduce the serialization of the List<> object of the simple method, in accordance with their own circumstances.