XML 和 List 互轉類
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace XmlHelper
{
/// <summary>
/// 實體轉Xml,Xml轉實體類
/// </summary>
/// <typeparam name="T"></typeparam>
public class XmlHelper<T> where T : new()
{
#region 實體類轉成Xml
/// <summary>
/// 對象執行個體轉成xml
/// </summary>
/// <param name="item">對象執行個體</param>
/// <returns></returns>
public static string EntityToXml(T item)
{
IList<T> items = new List<T>();
items.Add(item);
return EntityToXml(items);
}
/// 對象執行個體集轉成xml
/// <param name="items">對象執行個體集</param>
public static string EntityToXml(IList<T> items)
//建立XmlDocument文檔
XmlDocument doc = new XmlDocument();
//建立根元素
XmlElement root = doc.CreateElement(typeof(T).Name + "s");
//添加根元素的子元素集
foreach (T item in items)
{
EntityToXml(doc, root, item);
}
//向XmlDocument文檔添加根元素
doc.AppendChild(root);
return doc.InnerXml;
private static void EntityToXml(XmlDocument doc, XmlElement root, T item)
//建立元素
XmlElement xmlItem = doc.CreateElement(typeof(T).Name);
//對象的屬性集
System.Reflection.PropertyInfo[] propertyInfo =
typeof(T).GetProperties(System.Reflection.BindingFlags.Public |
System.Reflection.BindingFlags.Instance);
foreach (System.Reflection.PropertyInfo pinfo in propertyInfo)
if (pinfo != null)
{
//對象屬性名稱
string name = pinfo.Name;
//對象屬性值
string value = String.Empty;
if (pinfo.GetValue(item, null) != null)
value = pinfo.GetValue(item, null).ToString();//擷取對象屬性值
//設定元素的屬性值
xmlItem.SetAttribute(name, value);
}
//向根添加子元素
root.AppendChild(xmlItem);
#endregion
#region Xml轉成實體類
/// Xml轉成對象執行個體
/// <param name="xml">xml</param>
public static T XmlToEntity(string xml)
IList<T> items = XmlToEntityList(xml);
if (items != null && items.Count > 0)
return items[0];
else return default(T);
/// Xml轉成對象執行個體集
public static IList<T> XmlToEntityList(string xml)
try
doc.LoadXml(xml);
catch
return null;
if (doc.ChildNodes.Count != 1)
if (doc.ChildNodes[0].Name.ToLower() != typeof(T).Name.ToLower() + "s")
XmlNode node = doc.ChildNodes[0];
foreach (XmlNode child in node.ChildNodes)
if (child.Name.ToLower() == typeof(T).Name.ToLower())
items.Add(XmlNodeToEntity(child));
return items;
private static T XmlNodeToEntity(XmlNode node)
T item = new T();
if (node.NodeType == XmlNodeType.Element)
XmlElement element = (XmlElement)node;
System.Reflection.PropertyInfo[] propertyInfo =
foreach (XmlAttribute attr in element.Attributes)
string attrName = attr.Name.ToLower();
string attrValue = attr.Value.ToString();
foreach (System.Reflection.PropertyInfo pinfo in propertyInfo)
{
if (pinfo != null)
{
string name = pinfo.Name.ToLower();
Type dbType = pinfo.PropertyType;
if (name == attrName)
{
if (String.IsNullOrEmpty(attrValue))
continue;
switch (dbType.ToString())
{
case "System.Int32":
pinfo.SetValue(item, Convert.ToInt32(attrValue), null);
break;
case "System.Boolean":
pinfo.SetValue(item, Convert.ToBoolean(attrValue), null);
case "System.DateTime":
pinfo.SetValue(item, Convert.ToDateTime(attrValue), null);
case "System.Decimal":
pinfo.SetValue(item, Convert.ToDecimal(attrValue), null);
case "System.Double":
pinfo.SetValue(item, Convert.ToDouble(attrValue), null);
default:
pinfo.SetValue(item, attrValue, null);
}
continue;
}
}
}
return item;
}
}
XML轉List
List轉XML
Model轉XML
XML轉Model