zoukankan      html  css  js  c++  java
  • C# 对象转XML 支持匿名类

    在网上找了很多关于对象转XML的,大多不支持匿名类转换,今天在stackoverflow找了一篇文章  但是有些许BUG  已经修复

     1 public static class ObjectToXML
     2     {
     3         private static readonly Type[] WriteTypes = new[] {
     4         typeof(string), typeof(DateTime),typeof(decimal), typeof(Guid),
     5         };
     6         public static bool IsSimpleType(this Type type)
     7         {
     8             return type.IsPrimitive || WriteTypes.Contains(type) || type.IsEnum;
     9         }
    10         public static XElement ToXml(this object input)
    11         {
    12             return input.ToXml(null);
    13         }
    14 
    15         private static string GetXMLElementAttributeName(PropertyInfo info)
    16         {
    17             string attributeName = "";
    18 
    19             var attr = info.GetCustomAttributes(true);
    20             if (attr != null && attr.Length > 0)
    21             {
    22                 
    23                 foreach (var item in attr)
    24                 {
    25                     if (item is XmlElementAttribute)
    26                     {
    27                         var temp = item as XmlElementAttribute;
    28                         attributeName = temp.ElementName;
    29 
    30                         
    31                     }
    32                     else if(item is XmlRootAttribute)
    33                     {
    34                         var temp = item as XmlRootAttribute;
    35                         attributeName = temp.ElementName;
    36                     }
    37                 }
    38             }
    39             return attributeName;
    40         }
    41 
    42         private static object GetPropertyValue(object input, PropertyInfo info)
    43         {
    44             if (info.PropertyType.IsEnum)
    45             {
    46                 return (int)info.GetValue(input);
    47             }
    48             return info.GetValue(input);
    49         }
    50 
    51         public static XElement ToXml(this object input, string element)
    52         {
    53             if (input == null)
    54                 return null;
    55 
    56             if (string.IsNullOrEmpty(element))
    57                 element = "object";
    58 
    59             element = XmlConvert.EncodeName(element);
    60             var ret = new XElement(element);
    61 
    62             if (input != null)
    63             {
    64                 var type = input.GetType();
    65                 var props = type.GetProperties();
    66                 var elements = from prop in props
    67                                let name = XmlConvert.EncodeName(GetXMLElementAttributeName(prop) == "" ? prop.Name : GetXMLElementAttributeName(prop))
    68                                let val = GetPropertyValue(input,prop)
    69                                let value = prop.PropertyType.IsSimpleType()
    70                                     ? new XElement(name, val)
    71                                     : val.ToXml(name)
    72                                where value != null
    73                                select value;
    74 
    75                 ret.Add(elements);
    76             }
    77 
    78             return ret;
    79         }
    80     }

    调用:

    var model = new {
       Name = "张三",
       Age = 18 
    };
    
    model.ToXml("RequestParameter").ToString();

    记得引入命名空间

  • 相关阅读:
    easyui源码翻译1.32--Droppable(放置)
    easyui源码翻译1.32--Draggable(拖动)
    easyui源码翻译1.32--Dialog(对话框窗口)
    easyui源码翻译1.32--DateTimeBox(日期时间输入框)
    easyui源码翻译1.32--DateBox(日期输入框)
    easyui源码翻译1.32--ComboTree(树形下拉框)
    easyui源码翻译1.32--ComboGrid(数据表格下拉框)
    我不曾忘记的初心-大厂小厂
    我不曾忘记的初心-屌丝逆袭
    我不曾忘记的初心-愿天堂没有代码
  • 原文地址:https://www.cnblogs.com/cx36727/p/10689222.html
Copyright © 2011-2022 走看看