欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C#教程

當(dāng)前位置:主頁 > 軟件編程 > C#教程 >

C#中把任意類型的泛型集合轉(zhuǎn)換成SQLXML數(shù)據(jù)格式的實(shí)例

來源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C#教程|點(diǎn)擊: 次

話不多說,跟著小編一起來看下吧

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>
  /// 泛型轉(zhuǎn)換類
  /// </summary>
  static class GenericConver
  {
   /// <summary>
   /// 集合轉(zhuǎn)換成SQLXML
   /// </summary>
   /// <typeparam name="T">泛型參數(shù)(集合成員的類型)</typeparam>
   /// <param name="TCollection">泛型集合</param>
   /// <returns></returns>
   public static SqlXml CollectionToSqlXml<T>(IEnumerable<T> TCollection)
   {
    //先把集合轉(zhuǎn)換成數(shù)據(jù)表,然后把數(shù)據(jù)表轉(zhuǎn)換成SQLXML
    return DataTableToSqlXml(CollectionToDataTable(TCollection));
   }
   /// <summary>
   /// 集合轉(zhuǎn)換成數(shù)據(jù)表
   /// </summary>
   /// <typeparam name="T">泛型參數(shù)(集合成員的類型)</typeparam>
   /// <param name="TCollection">泛型集合</param>
   /// <returns></returns>
   public static DataTable CollectionToDataTable<T>(IEnumerable<T> TCollection)
   {
    //獲取泛型的具體類型
    Type type = typeof(T);
    //獲取類型的公共屬性
    PropertyInfo[] properties = type.GetProperties();
    //創(chuàng)建數(shù)據(jù)表,表名為類型名稱
    DataTable table = new DataTable(type.Name);
    //把公共屬性轉(zhuǎn)行成表格列,再把表格列添加到表格中
    foreach (var property in properties)
    {
     //創(chuàng)建一個(gè)表格列,列名為屬性名,列數(shù)據(jù)類型為屬性的類型
     DataColumn column = new DataColumn(property.Name, property.PropertyType);
     //把表格列添加到表格中
     table.Columns.Add(column);
    }
    //把泛型集合元素添加到數(shù)據(jù)行中
    foreach (var item in TCollection)
    {
     //創(chuàng)建和表格行架構(gòu)相同的表格行
     DataRow row = table.NewRow();
     //讀取元素所有屬性列的值,并根據(jù)屬性名稱,把屬性值添加到表格行中
     foreach (var property in properties)
      row[property.Name] = property.GetValue(item, null);
     //把表格行添加到表格中
     table.Rows.Add(row);
    }
    return table;
   }
   /// <summary>
   /// 數(shù)據(jù)表轉(zhuǎn)換成SQLXML
   /// </summary>
   /// <param name="table">數(shù)據(jù)表</param>
   /// <returns></returns>
   public static SqlXml DataTableToSqlXml(DataTable table)
   {
    SqlXml xml;
    //如果表格名為空,則設(shè)置表格名
    if (string.IsNullOrEmpty(table.TableName))
     table.TableName = "TableName";
    //把數(shù)據(jù)表轉(zhuǎn)換成XML
    using (var ms = new MemoryStream())
    {
     //把數(shù)據(jù)表轉(zhuǎn)換成XML格式,并寫入內(nèi)存流
     table.WriteXml(ms);
     //把內(nèi)存流讀取標(biāo)記設(shè)置回起點(diǎn)
     ms.Position = 0;
     //使用XmlReader讀取內(nèi)存流,并創(chuàng)建一個(gè)SqlXml對(duì)象
     xml = new SqlXml(XmlReader.Create(ms));
    }
    return xml;
   }
  }
  /// <summary>
  /// 人類(測(cè)試數(shù)據(jù)類)
  /// </summary>
  class Person
  {
   /// <summary>
   /// 構(gòu)造函數(shù)
   /// </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; }
  }
 }
}

輸出結(jié)果:

<DocumentElement>
 <Person>
 <Name>李元芳</Name>
 <Age>23</Age>
 </Person>
 <Person>
 <Name>狄仁杰</Name>
 <Age>32</Age>
 </Person>
</DocumentElement>

主要是通過反射,讀取泛型類型的屬性,然后根據(jù)讀取到的屬性生成數(shù)據(jù)表,再把數(shù)據(jù)表轉(zhuǎn)換成XML格式。

注釋已經(jīng)寫得很詳盡了,我也不知道還需要說明點(diǎn)什么,如果這個(gè)小例子能幫到誰的小忙就最好不過了哈~

以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持我們!

上一篇:C#中abstract的用法詳解

欄    目:C#教程

下一篇:詳解C# Socket編程筆記

本文標(biāo)題:C#中把任意類型的泛型集合轉(zhuǎn)換成SQLXML數(shù)據(jù)格式的實(shí)例

本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6088.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有