加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

XML 和 List 互转类

发布时间:2020-12-15 22:28:39 所属栏目:百科 来源:网络整理
导读:XML 和 List 互转类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; namespace XmlHelper { /// summary 实体转Xml,Xml转实体类 /summary typeparamname="T"/typeparam public class XmlHelper

XML 和 List 互转类


usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Xml;

namespaceXmlHelper
{
/// <summary>
实体转Xml,Xml转实体类
</summary> <typeparamname="T"></typeparam>
public classXmlHelper<T> whereT: new()
{
#region实体类转成Xml
对象实例转成xml
<paramname="item"> 对象实例 </param> <returns></returns> static stringEntityToXml(Titem)
{
IList<T>items= newList<T>();
items.Add(item);
returnEntityToXml(items);
}

对象实例集转成xml
<paramname="items"> 对象实例集 stringEntityToXml(IList<T>items)
{
// 创建XmlDocument文档
XmlDocumentdoc= newXmlDocument();
创建根元素
XmlElementroot=doc.CreateElement( typeof(T).Name+ " s ");
添加根元素的子元素集
foreach(Titem initems)
{
EntityToXml(doc,root,item);
}
向XmlDocument文档添加根元素
doc.AppendChild(root);

returndoc.InnerXml;
}

private voidEntityToXml(XmlDocumentdoc,XmlElementroot,Titem)
{
创建元素
XmlElementxmlItem=doc.CreateElement( typeof(T).Name);
对象的属性集

System.Reflection.PropertyInfo[]propertyInfo=
typeof(T).GetProperties(System.Reflection.BindingFlags.Public|
System.Reflection.BindingFlags.Instance);



foreach(System.Reflection.PropertyInfopinfo inpropertyInfo)
{
if(pinfo!= null)
{
对象属性名称 stringname=pinfo.Name;
对象属性值 stringvalue=String.Empty;

if(pinfo.GetValue(item,null)!= null)
value=pinfo.GetValue(item,255)">null).ToString(); 获取对象属性值
设置元素的属性值
xmlItem.SetAttribute(name,value);
}
}
向根添加子元素
root.AppendChild(xmlItem);
}


#endregion

#regionXml转成实体类

Xml转成对象实例
<paramname="xml"> xml staticTXmlToEntity( stringxml)
{
IList<T>items=XmlToEntityList(xml);
if(items!= null&&items.Count> 0)
returnitems[ 0];
else return default(T);
}

Xml转成对象实例集
staticIList<T>XmlToEntityList( stringxml)
{
XmlDocumentdoc= try
{
doc.LoadXml(xml);
}
catch
{
null;
}
if(doc.ChildNodes.Count!= 1)
null;
if(doc.ChildNodes[ 0].Name.ToLower()!= typeof(T).Name.ToLower()+ ")
null;

XmlNodenode=doc.ChildNodes[ 0];

IList<T>items= newList<T>();

foreach(XmlNodechild innode.ChildNodes)
{
if(child.Name.ToLower()== typeof(T).Name.ToLower())
items.Add(XmlNodeToEntity(child));
}

returnitems;
}

staticTXmlNodeToEntity(XmlNodenode)
{
Titem= newT();

if(node.NodeType==XmlNodeType.Element)
{
XmlElementelement=(XmlElement)node;

System.Reflection.PropertyInfo[]propertyInfo=
typeof(T).GetProperties(System.Reflection.BindingFlags.Public|
System.Reflection.BindingFlags.Instance);

foreach(XmlAttributeattr inelement.Attributes)
{
stringattrName=attr.Name.ToLower();
stringattrValue=attr.Value.ToString();
stringname=pinfo.Name.ToLower();
TypedbType=pinfo.PropertyType;
if(name==attrName)
{
if(String.IsNullOrEmpty(attrValue))
continue;
switch(dbType.ToString())
{
case System.Int32 ":
pinfo.SetValue(item,Convert.ToInt32(attrValue),255)">null);
break;
System.Boolean System.DateTime System.Decimal System.Double default:
pinfo.SetValue(item,attrValue,255)">break;
}
continue;
}
}
}
}
}
returnitem;
}

#endregion
}

}

XML转List

List转XML

Model转XML

XML转Model

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读