C#中利用LINQ to XML与反射把任意类型的泛型集合转换成XML格式字符串的方法
作者:弎吩锺熱℃ 时间:2022-02-12 23:04:39
在工作中,如果需要跟XML打交道,难免会遇到需要把一个类型集合转换成XML格式的情况。之前的方法比较笨拙,需要给不同的类型,各自写一个转换的函数。但是后来接触反射后,就知道可以利用反射去读取一个类型的所有成员,也就意味着可以替不同的类型,创建更通用的方法。这个例子是这样做的:利用反射,读取一个类型的所有属性,然后再把属性转换成XML元素的属性或者子元素。下面注释比较完整,就话不多说了,有需要看代码吧!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Reflection;
namespace GenericCollectionToXml
{
class Program
{
static void Main(string[] args)
{
var persons = new[]{
new Person(){Name="李元芳",Age=23},
new Person(){Name="狄仁杰",Age=32}
};
Console.WriteLine(CollectionToXml(persons));
}
/// <summary>
/// 集合转换成数据表
/// </summary>
/// <typeparam name="T">泛型参数(集合成员的类型)</typeparam>
/// <param name="TCollection">泛型集合</param>
/// <returns>集合的XML格式字符串</returns>
public static string CollectionToXml<T>(IEnumerable<T> TCollection)
{
//定义元素数组
var elements = new List<XElement>();
//把集合中的元素添加到元素数组中
foreach (var item in TCollection)
{
//获取泛型的具体类型
Type type = typeof(T);
//定义属性数组,XObject是XAttribute和XElement的基类
var attributes = new List<XObject>();
//获取类型的所有属性,并把属性和值添加到属性数组中
foreach (var property in type.GetProperties())
//获取属性名称和属性值,添加到属性数组中(也可以作为子元素添加到属性数组中,只需把XAttribute更改为XElement)
attributes.Add(new XAttribute(property.Name, property.GetValue(item, null)));
//把属性数组添加到元素中
elements.Add(new XElement(type.Name, attributes));
}
//初始化根元素,并把元素数组作为根元素的子元素,返回根元素的字符串格式(XML)
return new XElement("Root", elements).ToString();
}
/// <summary>
/// 人类(测试数据类)
/// </summary>
class Person
{
/// <summary>
/// 名称
/// </summary>
public string Name { get; set; }
/// <summary>
/// 年龄
/// </summary>
public int Age { get; set; }
}
}
}
把属性作为属性输出:
<Root>
<Person Name="李元芳" Age="23" />
<Person Name="狄仁杰" Age="32" />
</Root>
把属性作为子元素输出:
<Root> <Person> <Name>李元芳</Name> <Age>23</Age> </Person> <Person> <Name>狄仁杰</Name> <Age>32</Age> </Person></Root>
标签:C#,泛型集合,字符串
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java的Socket网络编程基础知识入门教程
2021-11-19 10:10:37
C#对文件名智能排序的算法
2022-05-02 16:18:28
c#如何实现接口事件
2023-10-02 18:31:28
详解SpringSecurity中的Authentication信息与登录流程
2022-01-20 21:36:22
![](https://img.aspxhome.com/file/2023/4/78864_0s.png)
java使用hadoop实现关联商品统计
2022-11-05 05:55:43
![](https://img.aspxhome.com/file/2023/3/76603_0s.jpg)
SpringBoot 将配置文件挂到 jar 包外面的操作方法
2023-11-17 11:09:32
springboot 在idea中实现热部署的方法
2022-10-24 22:27:19
![](https://img.aspxhome.com/file/2023/8/70518_0s.png)
java开发之内部类的用法
2023-02-04 21:30:07
Maven之导入thymeleaf依赖飘红问题及解决
2023-11-12 12:49:05
![](https://img.aspxhome.com/file/2023/0/79920_0s.png)
Java中抽象类与方法的重写方式
2023-08-25 09:20:29
springboot 中整合mybatis多数据源不使用JPA
2023-03-01 08:43:02
![](https://img.aspxhome.com/file/2023/3/61823_0s.png)
SpringMvc+Mybatis+Pagehelper分页详解
2021-08-13 14:15:11
spring拓展之如何定义自己的namespace
2022-01-12 05:13:33
![](https://img.aspxhome.com/file/2023/3/79873_0s.png)
避免sql注入_动力节点Java学院整理
2023-08-21 17:39:12
C#基础之异步调用实例教程
2021-07-03 08:44:56
c# 获取照片的经纬度和时间的示例代码
2022-03-22 18:21:54
身份证号码验证算法深入研究和Java实现
2023-05-18 08:17:42
如何基于FTP4J实现FTPS连接过程解析
2022-09-19 21:51:35
![](https://img.aspxhome.com/file/2023/5/80315_0s.jpg)
C# 最基础知识介绍--多态
2022-07-10 14:34:36
![](https://img.aspxhome.com/file/2023/1/79111_0s.jpg)
Java泛型与数据库应用实例详解
2023-08-14 09:37:15
![](https://img.aspxhome.com/file/2023/4/64014_0s.png)