实体集合和实体转换成相应的string、XDocument、XElement、XDocument

一、前言

上篇随笔主要是针对于Xml的特性Attribute与实体之间的匹配与转换。该篇随笔主要内容为将对象转换成相应的Xml以及XElement。这2篇随笔以不同的方式对Xml进行转换与匹配,每种匹配都采用不同的角度进行操作。本文主要为对象实体的转换,下篇侧重于Xml的匹配。

二、Xml转换

2.1 实体集合转换Xml

实体集合转换Xml的方法为:public static string ToXml<T>(IList<T> entities, string rootName = "") where T : new(),通过传入的实体集合对象和Xml根名称,可以转换成相应的Xml,代码如下:

 1         public  static  string ToXml<T>(IList<T> entities,  string rootName =  ""where T :  new()
 2         {
 3              if (entities ==  null || entities.Count ==  0)
 4             {
 5                  return  string.Empty;
 6             }
 7 
 8             StringBuilder builder =  new StringBuilder();
 9             builder.AppendLine(XmlResource.XmlHeader);
10 
11             XElement element = ToXElement<T>(entities, rootName);
12             builder.Append(element.ToString());
13 
14              return builder.ToString();
15         }

 针对于实体集合的转换,转换后的结果如下:

 1 <?xml version= " 1.0" encoding="utf-8" ?>
 2  <MapperInfoSet>
 3   <MapperInfo>
 4     <Name>MapperInfoIndex0</Name>
 5     <CreatedTime> 2012- 02-19T08: 54: 44.9411601+ 08: 00</CreatedTime>
 6     <IsActive> true</IsActive>
 7     <Value> 0</Value>
 8     <Percent> 50</Percent>
 9     <TargetUrl>www.codeplex.com?Id= 0</TargetUrl>
10   </MapperInfo>
11   <MapperInfo>
12     <Name>MapperInfoIndex1</Name>
13     <CreatedTime> 2012- 02-19T08: 54: 44.9421602+ 08: 00</CreatedTime>
14     <IsActive> false</IsActive>
15     <Value> 1</Value>
16     <Percent> 50</Percent>
17     <TargetUrl>www.codeplex.com?Id= 1</TargetUrl>
18   </MapperInfo>
19 </MapperInfoSet>

 

2.2 实体转换Xml

实体转换Xml的方法为:public static string ToXml<T>(T entity) where T : new(),通过传入的实体,可以转换成相应的Xml,代码如下:

 1          public  static  string ToXml<T>(T entity)  where T :  new()
 2         {
 3              if (entity ==  null)
 4             {
 5                  return  string.Empty;
 6             }
 7 
 8             XElement element = ToXElement<T>(entity);
 9 
10              return element.ToString();
11         }

针对于单个实体的转换,转换后的结果如下:

1 <MapperInfo>
2   <Name>MapperInfoIndex0</Name>
3   <CreatedTime> 2012- 02-19T08: 59: 17.1387289+ 08: 00</CreatedTime>
4   <IsActive> true</IsActive>
5   <Value> 0</Value>
6   <Percent> 50</Percent>
7   <TargetUrl>www.codeplex.com?Id= 0</TargetUrl>
8 </MapperInfo>

 

2.3 实体集合转换XElement

实体转换XElement的方法为:public static XElement ToXElement<T>(IList<T> entities, string rootName = "") where T : new(),通过传入的实体集合对象和Xml根名称,可以转换成相应的XElement,代码如下:

 1          public  static XElement ToXElement<T>(IList<T> entities,  string rootName =  ""where T :  new()
 2         {
 3              if (entities ==  null || entities.Count ==  0)
 4             {
 5                  return  null;
 6             }
 7 
 8              if ( string.IsNullOrWhiteSpace(rootName))
 9             {
10                 rootName =  typeof(T).Name + XmlResource.XmlRootNameSuffix;
11             }
12 
13             XElement element =  new XElement(rootName);
14 
15              foreach (T entity  in entities)
16             {
17                 element.Add(ToXElement<T>(entity));
18             }
19 
20              return element;
21         }

 

2.4 实体集合转换XmlDocument

实体转换XmlDocument的方法为:public static XElement ToXmlDocument<T>(IList<T> entities, string rootName = "") where T : new(),通过传入的实体集合对象和Xml根名称,可以转换成相应的XmlDocument,代码如下:

 1          public  static XmlDocument ToXmlDocument<T>(IList<T> entities,  string rootName =  ""where T :  new()
 2         {
 3              if (entities ==  null || entities.Count ==  0)
 4             {
 5                  return  null;
 6             }
 7 
 8             XmlDocument xmlDocument =  new XmlDocument();
 9             xmlDocument.LoadXml(ToXml<T>(entities, rootName));
10 
11              return xmlDocument;
12         }

 

2.5 实体转换XElement

实体转换XElement的方法为:public static string ToXElement<T>(T entity) where T : new(),通过传入的实体,可以转换成相应的XElement,代码如下:

 1          public  static XElement ToXElement<T>(T entity)  where T :  new()
 2         {
 3              if (entity ==  null)
 4             {
 5                  return  null;
 6             }
 7 
 8             XElement element =  new XElement( typeof(T).Name);
 9             PropertyInfo[] properties =  typeof(T).GetProperties();
10             XElement innerElement =  null;
11              object propertyValue =  null;
12 
13              foreach (PropertyInfo property  in properties)
14             {
15                 propertyValue = property.GetValue(entity,  null);
16                 innerElement =  new XElement(property.Name, propertyValue);
17                 element.Add(innerElement);
18             }
19 
20              return element;
21         }

 

2.6 实体集合转换XDocument

实体转换XDocument的方法为:public static XDocument ToXDocument<T>(IList<T> entities, string rootName = "") where T : new(),通过传入的实体集合对象和Xml根名称,可以转换成相应的XDocument,代码如下:

1          public  static XDocument ToXDocument<T>(IList<T> entities,  string rootName =  ""where T :  new()
2         {
3              if (entities ==  null || entities.Count ==  0)
4             {
5                  return  null;
6             }
7           
8              return  XDocument.Parse(ToXml<T>(entities, rootName));
9         }

 

 三、总结

以上的代码很少,主要通过重构来使代码简化。当然,将实体集合和实体转换为相应的string、XDocument、XElement、XDocument是非常简单的。单元测试的代码就不贴了,占地方。下篇随笔主要是如何将本文中转换的Xml进行匹配,本文所有的代码如下:

ExpandedBlockStart.gif View Code
     public  class SimpleXmlConverter
    {
         public  static  string ToXml<T>(IList<T> entities,  string rootName =  ""where T :  new()
        {
             if (entities ==  null || entities.Count ==  0)
            {
                 return  string.Empty;
            }

            StringBuilder builder =  new StringBuilder();
            builder.AppendLine(XmlResource.XmlHeader);

            XElement element = ToXElement<T>(entities, rootName);
            builder.Append(element.ToString());

             return builder.ToString();
        }

         public  static XmlDocument ToXmlDocument<T>(IList<T> entities,  string rootName =  ""where T :  new()
        {
             if (entities ==  null || entities.Count ==  0)
            {
                 return  null;
            }

            XmlDocument xmlDocument =  new XmlDocument();
            xmlDocument.LoadXml(ToXml<T>(entities, rootName));

             return xmlDocument;
        }

         public  static XDocument ToXDocument<T>(IList<T> entities,  string rootName =  ""where T :  new()
        {
             if (entities ==  null || entities.Count ==  0)
            {
                 return  null;
            }
          
             return  XDocument.Parse(ToXml<T>(entities, rootName));
        }

         public  static XElement ToXElement<T>(IList<T> entities,  string rootName =  ""where T :  new()
        {
             if (entities ==  null || entities.Count ==  0)
            {
                 return  null;
            }
             
             if ( string.IsNullOrWhiteSpace(rootName))
            {
                rootName =  typeof(T).Name + XmlResource.XmlRootNameSuffix;
            }

            XElement element =  new XElement(rootName);

             foreach (T entity  in entities)
            {
                element.Add(ToXElement<T>(entity));
            }

             return element;
        }

         public  static  string ToXml<T>(T entity)  where T :  new()
        {
             if (entity ==  null)
            {
                 return  string.Empty;
            }

            XElement element = ToXElement<T>(entity);

             return element.ToString();
        }

         public  static XElement ToXElement<T>(T entity)  where T :  new()
        {
             if (entity ==  null)
            {
                 return  null;
            }

            XElement element =  new XElement( typeof(T).Name);
            PropertyInfo[] properties =  typeof(T).GetProperties();
            XElement innerElement =  null;
             object propertyValue =  null;

             foreach (PropertyInfo property  in properties)
            {
                propertyValue = property.GetValue(entity,  null);
                innerElement =  new XElement(property.Name, propertyValue);
                element.Add(innerElement);
            }

             return element;
        }

         public  static XElement ToXElement(Type type) 
        {
             if (type ==  null)
            {
                 return  null;
            }

            XElement element =  new XElement(type.Name);
            PropertyInfo[] properties = type.GetProperties();
            XElement innerElement =  null;

             foreach (PropertyInfo property  in properties)
            {
                innerElement =  new XElement(property.Name,  null);
                element.Add(innerElement);
            }

             return element;
        }
    }

 

转载于:https://www.cnblogs.com/jasenkin/archive/2012/02/19/simpe_xml_converter.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值