博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
XML 和 List 互转类
阅读量:7254 次
发布时间:2019-06-29

本文共 3821 字,大约阅读时间需要 12 分钟。

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);
        }
        
///
 
<summary>
        
///
 对象实例集转成xml
        
///
 
</summary>
        
///
 
<param name="items">
对象实例集
</param>
        
///
 
<returns></returns>
        
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转成实体类
        
///
 
<summary>
        
///
 Xml转成对象实例
        
///
 
</summary>
        
///
 
<param name="xml">
xml
</param>
        
///
 
<returns></returns>
        
public 
static T XmlToEntity(
string xml)
        {
            IList<T> items = XmlToEntityList(xml);
            
if (items != 
null && items.Count > 
0)
                
return items[
0];
            
else 
return 
default(T);
        }
        
///
 
<summary>
        
///
 Xml转成对象实例集
        
///
 
</summary>
        
///
 
<param name="xml">
xml
</param>
        
///
 
<returns></returns>
        
public 
static IList<T> XmlToEntityList(
string xml)
        {
            XmlDocument doc = 
new XmlDocument();
            
try
            {
                doc.LoadXml(xml);
            }
            
catch
            {
                
return 
null;
            }
            
if (doc.ChildNodes.Count != 
1)
                
return 
null;
            
if (doc.ChildNodes[
0].Name.ToLower() != 
typeof(T).Name.ToLower() + 
"
s
")
                
return 
null;
            XmlNode node = doc.ChildNodes[
0];
            IList<T> items = 
new List<T>();
            
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 =
            
typeof(T).GetProperties(System.Reflection.BindingFlags.Public |
            System.Reflection.BindingFlags.Instance);
                
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);
                                        
break;
                                    
case 
"
System.DateTime
":
                                        pinfo.SetValue(item, Convert.ToDateTime(attrValue), 
null);
                                        
break;
                                    
case 
"
System.Decimal
":
                                        pinfo.SetValue(item, Convert.ToDecimal(attrValue), 
null);
                                        
break;
                                    
case 
"
System.Double
":
                                        pinfo.SetValue(item, Convert.ToDouble(attrValue), 
null);
                                        
break;
                                    
default:
                                        pinfo.SetValue(item, attrValue, 
null);
                                        
break;
                                }
                                
continue;
                            }
                        }
                    }
                }
            }
            
return item;
        }
        
#endregion
    }

 

XML转List

List转XML

Model转XML

XML转Model 

转载于:https://www.cnblogs.com/kevinGao/p/4188723.html

你可能感兴趣的文章
node+vue报错合辑
查看>>
Date——js 获取当前日期到之后一个月30天的日期区间
查看>>
RT-SA-2019-003 Cisco RV320 Unauthenticated Configuration Export
查看>>
Java线程练习
查看>>
Algs4-1.5.22Erods-renyi模型的倍率实验
查看>>
计算机硬件的组成、python的开发层面及语法介绍
查看>>
python面试知识点
查看>>
栅格布局
查看>>
Zimber 8.8.12卸载后重新安装报错解决办法
查看>>
Git 自救指南
查看>>
spring boot配置项目访问路径,配置拦截器,配置静态虚拟路径
查看>>
linux运维笔记
查看>>
Lake Counting (DFS)
查看>>
信息安全影响因素
查看>>
org.apache.poi 读excel空列跳过的问题
查看>>
hadoop安装
查看>>
作业-闰年流程图
查看>>
delegate事件委托
查看>>
Linux Process VS Thread VS LWP
查看>>
Linux命令——free
查看>>