zoukankan      html  css  js  c++  java
  • C#中把任意类型的泛型集合转换成SQLXML数据格式的小例子

      1 using System;
      2 using System.Collections.Generic;
      3 using System.Linq;
      4 using System.Text;
      5 using System.Data.SqlTypes;
      6 using System.Data;
      7 using System.Reflection;
      8 using System.IO;
      9 using System.Xml;
     10 
     11 namespace CollectionToXml
     12 {
     13     class Program
     14     {
     15         static void Main(string[] args)
     16         {
     17             //persons可替换为任何泛型集合
     18             var persons = new[] { 
     19                 new Person("李元芳", 23) , 
     20                 new Person("狄仁杰", 32) 
     21             };
     22             SqlXml sqlXml = GenericConver.CollectionToSqlXml(persons);
     23             Console.WriteLine(sqlXml.Value);
     24         }
     25 
     26         /// <summary>
     27         /// 泛型转换类
     28         /// </summary>
     29         static class GenericConver
     30         {
     31             /// <summary>
     32             /// 集合转换成SQLXML
     33             /// </summary>
     34             /// <typeparam name="T">泛型参数(集合成员的类型)</typeparam>
     35             /// <param name="TCollection">泛型集合</param>
     36             /// <returns></returns>
     37             public static SqlXml CollectionToSqlXml<T>(IEnumerable<T> TCollection)
     38             {
     39                 //先把集合转换成数据表,然后把数据表转换成SQLXML
     40                 return DataTableToSqlXml(CollectionToDataTable(TCollection));
     41             }
     42 
     43             /// <summary>
     44             /// 集合转换成数据表
     45             /// </summary>
     46             /// <typeparam name="T">泛型参数(集合成员的类型)</typeparam>
     47             /// <param name="TCollection">泛型集合</param>
     48             /// <returns></returns>
     49             public static DataTable CollectionToDataTable<T>(IEnumerable<T> TCollection)
     50             {
     51                 //获取泛型的具体类型
     52                 Type type = typeof(T);
     53                 //获取类型的公共属性
     54                 PropertyInfo[] properties = type.GetProperties();
     55                 //创建数据表,表名为类型名称
     56                 DataTable table = new DataTable(type.Name);
     57                 //把公共属性转行成表格列,再把表格列添加到表格中
     58                 foreach (var property in properties)
     59                 {
     60                     //创建一个表格列,列名为属性名,列数据类型为属性的类型
     61                     DataColumn column = new DataColumn(property.Name, property.PropertyType);
     62                     //把表格列添加到表格中
     63                     table.Columns.Add(column);
     64                 }
     65                 //把泛型集合元素添加到数据行中
     66                 foreach (var item in TCollection)
     67                 {
     68                     //创建和表格行架构相同的表格行
     69                     DataRow row = table.NewRow();
     70                     //读取元素所有属性列的值,并根据属性名称,把属性值添加到表格行中
     71                     foreach (var property in properties)
     72                         row[property.Name] = property.GetValue(item, null);
     73                     //把表格行添加到表格中
     74                     table.Rows.Add(row);
     75                 }
     76                 return table;
     77             }
     78             /// <summary>
     79             /// 数据表转换成SQLXML
     80             /// </summary>
     81             /// <param name="table">数据表</param>
     82             /// <returns></returns>
     83             public static SqlXml DataTableToSqlXml(DataTable table)
     84             {
     85                 SqlXml xml;
     86                 //如果表格名为空,则设置表格名
     87                 if (string.IsNullOrEmpty(table.TableName))
     88                     table.TableName = "TableName";
     89                 //把数据表转换成XML
     90                 using (var ms = new MemoryStream())
     91                 {
     92                     //把数据表转换成XML格式,并写入内存流
     93                     table.WriteXml(ms);
     94                     //把内存流读取标记设置回起点
     95                     ms.Position = 0;
     96                     //使用XmlReader读取内存流,并创建一个SqlXml对象
     97                     xml = new SqlXml(XmlReader.Create(ms));
     98                 }
     99                 return xml;
    100             }
    101         }
    102 
    103         /// <summary>
    104         /// 人类(测试数据类)
    105         /// </summary>
    106         class Person
    107         {
    108             /// <summary>
    109             /// 构造函数
    110             /// </summary>
    111             /// <param name="name">名称</param>
    112             /// <param name="age">年龄</param>
    113             public Person(string name, int age)
    114             { Name = name; Age = age; }
    115 
    116             /// <summary>
    117             /// 名称
    118             /// </summary>
    119             public string Name { get; set; }
    120 
    121             /// <summary>
    122             /// 年龄
    123             /// </summary>
    124             public int Age { get; set; }
    125         }
    126     }
    127 }

    输出结果:

     1 <DocumentElement>
     2   <Person>
     3     <Name>李元芳</Name>
     4     <Age>23</Age>
     5   </Person>
     6   <Person>
     7     <Name>狄仁杰</Name>
     8     <Age>32</Age>
     9   </Person>
    10 </DocumentElement>

    主要是通过反射,读取泛型类型的属性,然后根据读取到的属性生成数据表,再把数据表转换成XML格式。

    注释已经写得很详尽了,我也不知道还需要说明点什么,如果这个小例子能帮到谁的小忙就最好不过了哈~

  • 相关阅读:
    Linked List Cycle leetcode java (链表检测环)
    Remove Duplicates from Sorted List II leetcode java
    Remove Duplicates from Sorted List leetcode java
    Merge Two Sorted Lists leetcode java
    Swap Nodes in Pairs leetcode java
    Median of Two Sorted Array leetcode java
    阿里云最便宜的四种域名注册
    nohup和&后台运行,进程查看及终止
    ipv6转ipv4 NAT64与DNS64基本原理概述
    ros使用pppoe拨号获取ipv6,并且下发IPV6的dns到客户机win7
  • 原文地址:https://www.cnblogs.com/Tench/p/GenericCollectionConverToXml.html
Copyright © 2011-2022 走看看