用反射实现泛型集合与XML的序列化反序列化
分类: 小皮鞋代码专区
对于富实体的数据持久层,我们需要映射关系,与装载数据.在这里我们只讲数据库与数据实体传递的通道:即序列化与反序列化.
首先我们建一个泛型集合book,用它来做承载数据的容器.
using System
using System.Collections.Generic
using System.Text
using System.Collections
namespace WindowsApplication1
{
public class book<T> : List<T> where T:english,new ()
{
}
}
接下来我们建个数据实体,english:
private string bookname
public string Bookname
{
get { return bookname }
set { bookname = value }
}
private int count
public int Count
{
get { return count }
set { count = value }
}
下面让我们创建序列化与反序列化的执行容器serial:
using System
using System.Collections.Generic
using System.Text
using System.Reflection
using System.Xml
using System.IO
using System.Collections
namespace WindowsApplication1
{
public class serial
{
// 把泛型集合序列化为XML
public string objectToXml(book<english> books)
{
TextWriter TW = new StringWriter()
XmlTextWriter xmlTW = new XmlTextWriter(TW)
xmlTW.WriteStartElement("根节点")
foreach(english item in books)
{
xmlTW.WriteStartElement(typeof(english).FullName)
foreach (PropertyInfo pro in item.PropertyInfoList())
{
xmlTW.WriteStartElement(pro.Name)
xmlTW.WriteAttributeString("type", pro.PropertyType.FullName)
xmlTW.WriteAttributeString("value", pro.GetValue(item,null).ToString())
xmlTW.WriteEndElement()
}
xmlTW.WriteEndElement()
}
xmlTW.WriteEndElement()
return TW.ToString()
}
//把XML反序列化为泛型集合
public book<english> xmlToOjbect(string xml)
{
book<english> list = new book<english>()
XmlDocument xmlDC = new XmlDocument()
xmlDC.LoadXml(xml)
XmlNode root = xmlDC.DocumentElement
XmlNodeList nodes = root.SelectNodes(typeof(english).FullName)
foreach(XmlNode Node in nodes)
{
english item = new english()
Type typ = item.GetType().GetProperty(Node.Name).PropertyType;
item.GetType().GetProperty(Node.Name).SetValue(item, Convert.ChangeType(Node.Attributes[1].Value,typ), null);
list.Add(item)
}
return list
}
}
}
最后让我们调用它们:
//首先我们建一个对象集合,并为其赋值
book<english> list = new book<english>()
english item = new english()
item.Bookname = "java"
item.Count = 22
list
分类: 小皮鞋代码专区
对于富实体的数据持久层,我们需要映射关系,与装载数据.在这里我们只讲数据库与数据实体传递的通道:即序列化与反序列化.
首先我们建一个泛型集合book,用它来做承载数据的容器.
using System
using System.Collections.Generic
using System.Text
using System.Collections
namespace WindowsApplication1
{
public class book<T> : List<T> where T:english,new ()
{
}
}
接下来我们建个数据实体,english:
private string bookname
public string Bookname
{
get { return bookname }
set { bookname = value }
}
private int count
public int Count
{
get { return count }
set { count = value }
}
下面让我们创建序列化与反序列化的执行容器serial:
using System
using System.Collections.Generic
using System.Text
using System.Reflection
using System.Xml
using System.IO
using System.Collections
namespace WindowsApplication1
{
public class serial
{
// 把泛型集合序列化为XML
public string objectToXml(book<english> books)
{
TextWriter TW = new StringWriter()
XmlTextWriter xmlTW = new XmlTextWriter(TW)
xmlTW.WriteStartElement("根节点")
foreach(english item in books)
{
xmlTW.WriteStartElement(typeof(english).FullName)
foreach (PropertyInfo pro in item.PropertyInfoList())
{
xmlTW.WriteStartElement(pro.Name)
xmlTW.WriteAttributeString("type", pro.PropertyType.FullName)
xmlTW.WriteAttributeString("value", pro.GetValue(item,null).ToString())
xmlTW.WriteEndElement()
}
xmlTW.WriteEndElement()
}
xmlTW.WriteEndElement()
return TW.ToString()
}
//把XML反序列化为泛型集合
public book<english> xmlToOjbect(string xml)
{
book<english> list = new book<english>()
XmlDocument xmlDC = new XmlDocument()
xmlDC.LoadXml(xml)
XmlNode root = xmlDC.DocumentElement
XmlNodeList nodes = root.SelectNodes(typeof(english).FullName)
foreach(XmlNode Node in nodes)
{
english item = new english()
Type typ = item.GetType().GetProperty(Node.Name).PropertyType;
item.GetType().GetProperty(Node.Name).SetValue(item, Convert.ChangeType(Node.Attributes[1].Value,typ), null);
list.Add(item)
}
return list
}
}
}
最后让我们调用它们:
//首先我们建一个对象集合,并为其赋值
book<english> list = new book<english>()
english item = new english()
item.Bookname = "java"
item.Count = 22
list