C#中把任意类型的泛型集合转换成SQLXML数据格式的实例
作者:弎吩锺熱℃ 时间:2022-12-24 08:16:21
话不多说,跟着小编一起来看下吧
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlTypes;
using System.Data;
using System.Reflection;
using System.IO;
using System.Xml;
namespace CollectionToXml
{
class Program
{
static void Main(string[] args)
{
//persons可替换为任何泛型集合
var persons = new[] {
new Person("李元芳", 23) ,
new Person("狄仁杰", 32)
};
SqlXml sqlXml = GenericConver.CollectionToSqlXml(persons);
Console.WriteLine(sqlXml.Value);
}
/// <summary>
/// 泛型转换类
/// </summary>
static class GenericConver
{
/// <summary>
/// 集合转换成SQLXML
/// </summary>
/// <typeparam name="T">泛型参数(集合成员的类型)</typeparam>
/// <param name="TCollection">泛型集合</param>
/// <returns></returns>
public static SqlXml CollectionToSqlXml<T>(IEnumerable<T> TCollection)
{
//先把集合转换成数据表,然后把数据表转换成SQLXML
return DataTableToSqlXml(CollectionToDataTable(TCollection));
}
/// <summary>
/// 集合转换成数据表
/// </summary>
/// <typeparam name="T">泛型参数(集合成员的类型)</typeparam>
/// <param name="TCollection">泛型集合</param>
/// <returns></returns>
public static DataTable CollectionToDataTable<T>(IEnumerable<T> TCollection)
{
//获取泛型的具体类型
Type type = typeof(T);
//获取类型的公共属性
PropertyInfo[] properties = type.GetProperties();
//创建数据表,表名为类型名称
DataTable table = new DataTable(type.Name);
//把公共属性转行成表格列,再把表格列添加到表格中
foreach (var property in properties)
{
//创建一个表格列,列名为属性名,列数据类型为属性的类型
DataColumn column = new DataColumn(property.Name, property.PropertyType);
//把表格列添加到表格中
table.Columns.Add(column);
}
//把泛型集合元素添加到数据行中
foreach (var item in TCollection)
{
//创建和表格行架构相同的表格行
DataRow row = table.NewRow();
//读取元素所有属性列的值,并根据属性名称,把属性值添加到表格行中
foreach (var property in properties)
row[property.Name] = property.GetValue(item, null);
//把表格行添加到表格中
table.Rows.Add(row);
}
return table;
}
/// <summary>
/// 数据表转换成SQLXML
/// </summary>
/// <param name="table">数据表</param>
/// <returns></returns>
public static SqlXml DataTableToSqlXml(DataTable table)
{
SqlXml xml;
//如果表格名为空,则设置表格名
if (string.IsNullOrEmpty(table.TableName))
table.TableName = "TableName";
//把数据表转换成XML
using (var ms = new MemoryStream())
{
//把数据表转换成XML格式,并写入内存流
table.WriteXml(ms);
//把内存流读取标记设置回起点
ms.Position = 0;
//使用XmlReader读取内存流,并创建一个SqlXml对象
xml = new SqlXml(XmlReader.Create(ms));
}
return xml;
}
}
/// <summary>
/// 人类(测试数据类)
/// </summary>
class Person
{
/// <summary>
/// 构造函数
/// </summary>
/// <param name="name">名称</param>
/// <param name="age">年龄</param>
public Person(string name, int age)
{ Name = name; Age = age; }
/// <summary>
/// 名称
/// </summary>
public string Name { get; set; }
/// <summary>
/// 年龄
/// </summary>
public int Age { get; set; }
}
}
}
输出结果:
<DocumentElement>
<Person>
<Name>李元芳</Name>
<Age>23</Age>
</Person>
<Person>
<Name>狄仁杰</Name>
<Age>32</Age>
</Person>
</DocumentElement>
主要是通过反射,读取泛型类型的属性,然后根据读取到的属性生成数据表,再把数据表转换成XML格式。
注释已经写得很详尽了,我也不知道还需要说明点什么,如果这个小例子能帮到谁的小忙就最好不过了哈~
来源:http://www.cnblogs.com/Tench/p/GenericCollectionConverToXml.html
标签:C#,泛型集合,SQLXML数据格式
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
将本地的jar包打到Maven的仓库中实例
2022-08-29 13:38:28
Ubuntu搭建Java开发环境笔记
2023-10-10 14:27:49
Java 调用天气Webservice详解及实例代码
2021-10-09 21:59:03
java实现随机验证码图片生成
2022-12-08 02:27:00
![](https://img.aspxhome.com/file/2023/1/82301_0s.jpg)
MyBatis-Plus拦截器实现数据权限控制的示例
2022-12-10 05:10:17
Java并发编程之Fork/Join框架的理解
2023-09-30 14:14:01
![](https://img.aspxhome.com/file/2023/9/75509_0s.png)
Springboot @Validated和@Valid的区别及使用详解
2023-05-30 18:40:25
![](https://img.aspxhome.com/file/2023/6/64916_0s.png)
Java编程实现向文本文件中读取数据之Scanner用法示例
2022-08-01 03:38:57
Spring实战之协调作用域不同步的Bean操作示例
2023-01-23 02:48:10
Java 超详细讲解ThreadLocal类的使用
2021-11-13 05:58:29
![](https://img.aspxhome.com/file/2023/2/71012_0s.png)
基于idea 的 Java中的get/set方法之优雅的写法
2023-11-26 20:22:50
![](https://img.aspxhome.com/file/2023/2/60092_0s.jpg)
spring boot整合Shiro实现单点登录的示例代码
2023-04-07 01:17:56
![](https://img.aspxhome.com/file/2023/5/65145_0s.png)
SpringCloud可视化链路追踪系统Zipkin部署过程
2023-11-27 04:34:06
![](https://img.aspxhome.com/file/2023/5/60125_0s.png)
浅析Java编程中枚举类型的定义与使用
2021-07-04 23:46:16
Java中ArrayList和SubList的坑面试题
2023-09-21 06:56:21
![](https://img.aspxhome.com/file/2023/5/76625_0s.png)
Java抢红包的红包生成算法
2023-06-23 20:52:04
![](https://img.aspxhome.com/file/2023/2/57532_0s.jpg)
java字符串常用操作方法(查找、截取、分割)
2023-11-29 03:21:13
Spring-Data-JPA整合MySQL和配置的方法
2023-10-29 10:19:41
![](https://img.aspxhome.com/file/2023/4/58734_0s.png)
详解Java中运算符及用法
2023-11-29 08:17:57
SpringBoot 创建web项目并部署到外部Tomcat
2023-09-15 18:25:04