实体类与xml互相转换通用

本文介绍如何将实体类序列化为XML并从XML反序列化为实体类,包括XML转实体类和实体类转XML的具体实现。

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

// <summary>

/// 实体类序列化成xml
/// </summary>
/// <param name="enitities">实体.</param>
/// <param name="headtag">节点名称</param>
/// <returns></returns>
public static string ObjListToXml<T>(List<T> enitities, string headtag)
{
StringBuilder sb = new StringBuilder();
PropertyInfo[] propinfos = null;
sb.AppendLine("<?xml version=/"1.0/" encoding=/"utf-8/"?>");
sb.AppendLine("<"+headtag+">");
foreach (T obj in enitities)
{
//初始化propertyinfo
if (propinfos == null)
{
Type objtype = obj.GetType();
propinfos = objtype.GetProperties();
}

sb.AppendLine("<item>");
foreach (PropertyInfo propinfo in propinfos)
{
sb.Append("<");
sb.Append(propinfo.Name);
sb.Append(">");
sb.Append(propinfo.GetValue(obj, null));
sb.Append("</");
sb.Append(propinfo.Name);
sb.AppendLine(">");
}
sb.AppendLine("</item>");
}
sb.AppendLine("</" + headtag + ">");
return sb.ToString();
}

xml转行为实体类

/// <summary>
/// xml文件转化为实体类列表
/// </summary>
/// <typeparam name="T">实体名称</typeparam>
/// <param name="xml">您的xml文件</param>
/// <param name="headtag">xml头文件</param>
/// <returns>实体列表</returns>
public static List<T> XmlToObjList<T>(string xml, string headtag)
where T : new()
{

List<T> list = new List<T>();
XmlDocument doc = new XmlDocument();
PropertyInfo[] propinfos = null;
doc.LoadXml(xml);
//XmlNodeList nodelist = doc.SelectNodes(headtag);
XmlNodeList nodelist = doc.GetElementsByTagName(headtag);
foreach (XmlNode node in nodelist)
{
T entity = new T();
//初始化propertyinfo
if (propinfos == null)
{
Type objtype = entity.GetType();
propinfos = objtype.GetProperties();
}
//填充entity类的属性
foreach (PropertyInfo propinfo in propinfos)
{
//实体类字段首字母变成小写的
string name = propinfo.Name.Substring(0, 1) + propinfo.Name.Substring(1, propinfo.Name.Length - 1);
XmlNode cnode = node.SelectSingleNode(name);
string v = cnode.InnerText;
if (v != null)
propinfo.SetValue(entity, Convert.ChangeType(v, propinfo.PropertyType), null);
}
list.Add(entity);

}
return list;

}


/// 实体类序列化成xml
/// </summary>
/// <param name="enitities">实体.</param>
/// <param name="headtag">节点名称</param>
/// <returns></returns>
public static string ObjListToXml<T>(List<T> enitities, string headtag)
{
StringBuilder sb = new StringBuilder();
PropertyInfo[] propinfos = null;
sb.AppendLine("<?xml version=/"1.0/" encoding=/"utf-8/"?>");
sb.AppendLine("<"+headtag+">");
foreach (T obj in enitities)
{
//初始化propertyinfo
if (propinfos == null)
{
Type objtype = obj.GetType();
propinfos = objtype.GetProperties();
}

sb.AppendLine("<item>");
foreach (PropertyInfo propinfo in propinfos)
{
sb.Append("<");
sb.Append(propinfo.Name);
sb.Append(">");
sb.Append(propinfo.GetValue(obj, null));
sb.Append("</");
sb.Append(propinfo.Name);
sb.AppendLine(">");
}
sb.AppendLine("</item>");
}
sb.AppendLine("</" + headtag + ">");
return sb.ToString();
}

xml转行为实体类

/// <summary>
/// xml文件转化为实体类列表
/// </summary>
/// <typeparam name="T">实体名称</typeparam>
/// <param name="xml">您的xml文件</param>
/// <param name="headtag">xml头文件</param>
/// <returns>实体列表</returns>
public static List<T> XmlToObjList<T>(string xml, string headtag)
where T : new()
{

List<T> list = new List<T>();
XmlDocument doc = new XmlDocument();
PropertyInfo[] propinfos = null;
doc.LoadXml(xml);
//XmlNodeList nodelist = doc.SelectNodes(headtag);
XmlNodeList nodelist = doc.GetElementsByTagName(headtag);
foreach (XmlNode node in nodelist)
{
T entity = new T();
//初始化propertyinfo
if (propinfos == null)
{
Type objtype = entity.GetType();
propinfos = objtype.GetProperties();
}
//填充entity类的属性
foreach (PropertyInfo propinfo in propinfos)
{
//实体类字段首字母变成小写的
string name = propinfo.Name.Substring(0, 1) + propinfo.Name.Substring(1, propinfo.Name.Length - 1);
XmlNode cnode = node.SelectSingleNode(name);
string v = cnode.InnerText;
if (v != null)
propinfo.SetValue(entity, Convert.ChangeType(v, propinfo.PropertyType), null);
}
list.Add(entity);

}
return list;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值