• C#中利用LINQ to XML与反射把任意类型的泛型集合转换成XML格式字符串


    在工作中,如果需要跟XML打交道,难免会遇到需要把一个类型集合转换成XML格式的情况。之前的方法比较笨拙,需要给不同的类型,各自写一个转换的函数。但是后来接触反射后,就知道可以利用反射去读取一个类型的所有成员,也就意味着可以替不同的类型,创建更通用的方法。这个例子是这样做的:利用反射,读取一个类型的所有属性,然后再把属性转换成XML元素的属性或者子元素。下面注释比较完整,就话不多说了,有需要看代码吧!

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Xml.Linq;
     6 using System.Reflection;
     7 
     8 namespace GenericCollectionToXml
     9 {
    10     class Program
    11     {
    12         static void Main(string[] args)
    13         {
    14             var persons = new[]{
    15                 new Person(){Name="李元芳",Age=23},
    16                 new Person(){Name="狄仁杰",Age=32}
    17             };
    18             Console.WriteLine(CollectionToXml(persons));
    19         }
    20 
    21         /// <summary>
    22         /// 集合转换成数据表
    23         /// </summary>
    24         /// <typeparam name="T">泛型参数(集合成员的类型)</typeparam>
    25         /// <param name="TCollection">泛型集合</param>
    26         /// <returns>集合的XML格式字符串</returns>
    27         public static string CollectionToXml<T>(IEnumerable<T> TCollection)
    28         {
    29             //定义元素数组
    30             var elements = new List<XElement>();
    31             //把集合中的元素添加到元素数组中
    32             foreach (var item in TCollection)
    33             {
    34                 //获取泛型的具体类型
    35                 Type type = typeof(T);
    36                 //定义属性数组,XObject是XAttribute和XElement的基类
    37                 var attributes = new List<XObject>();
    38                 //获取类型的所有属性,并把属性和值添加到属性数组中
    39                 foreach (var property in type.GetProperties())
    40                     //获取属性名称和属性值,添加到属性数组中(也可以作为子元素添加到属性数组中,只需把XAttribute更改为XElement)
    41                     attributes.Add(new XAttribute(property.Name, property.GetValue(item, null)));
    42                 //把属性数组添加到元素中
    43                 elements.Add(new XElement(type.Name, attributes));
    44             }
    45             //初始化根元素,并把元素数组作为根元素的子元素,返回根元素的字符串格式(XML)
    46             return new XElement("Root", elements).ToString();
    47         }
    48 
    49         /// <summary>
    50         /// 人类(测试数据类)
    51         /// </summary>
    52         class Person
    53         {
    54             /// <summary>
    55             /// 名称
    56             /// </summary>
    57             public string Name { get; set; }
    58 
    59             /// <summary>
    60             /// 年龄
    61             /// </summary>
    62             public int Age { get; set; }
    63         }
    64     }
    65 }

    把属性作为属性输出:

    1 <Root>
    2   <Person Name="李元芳" Age="23" />
    3   <Person Name="狄仁杰" Age="32" />
    4 </Root>

    把属性作为子元素输出:

     1 <Root>
     2   <Person>
     3     <Name>李元芳</Name>
     4     <Age>23</Age>
     5   </Person>
     6   <Person>
     7     <Name>狄仁杰</Name>
     8     <Age>32</Age>
     9   </Person>
    10 </Root>
  • 相关阅读:
    <s:property>的用法(jsp获取action中的值或者方法)
    struts2 Action获取表单数据
    form标签中id和name属性的区别
    button和submit区别
    hibernate could not resolve property
    Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyExce
    宏定义#define和内联函数inline的区别
    线程绑定cpu
    posix系统线程调度-设置线程优先级
    std::lock_guard和std::unique_lock的区别
  • 原文地址:https://www.cnblogs.com/Tench/p/AnyGenericCollectionToXml.html
Copyright © 2020-2023  润新知