The method of persisting data objects by serializing and deserializing a collection of generic data entities.
When we usually use the database, we often encounter a problem, that is, we do not want the data entity objects to be inserted into the database, but we want to persist them. , then we can serialize it into
an XML string and save it to other places. Since the string is generated, it can be saved to any place we want to save it. For example, asp.net's ViewState, cookie, cache, etc.
First, we define a data entity class.
class Entity
{
publicEntity()
{}
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;
}
}
}
So insert it 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 successfully. Let's serialize it
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;
}
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 deserialize the xml file generated above and generate the List<Entity> object just now
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;
}
Then we call this method:
List<Entity> list = Deserialize<Entity>(str);
It's over.
This article only introduces you to a simple method of serializing List<> objects. You should use it according to your own situation.
http://www.cnblogs.com/kchen/archive/2006/11/04/550382.html