SoFunction
Updated on 2025-03-06

Examples of converting arbitrary type of generic collection into SQLXML data format in C#

Without further ado, let's take a look with me

using System;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
namespace CollectionToXml
{
 class Program
 {
  static void Main(string[] args)
  {
   //persons can be replaced with any generic collection   var persons = new[] { 
    new Person("Li Yuanfang", 23) , 
    new Person("Die Renjie", 32) 
   };
   SqlXml sqlXml = (persons);
   ();
  }
  /// <summary>
  /// Generic Conversion Class  /// </summary>
  static class GenericConver
  {
   /// <summary>
   /// Convert collections to SQLXML   /// </summary>
   /// <typeparam name="T">Generic Parameters (type of collection member)</typeparam>   /// <param name="TCollection">Generic Collection</param>   /// &lt;returns&gt;&lt;/returns&gt;
   public static SqlXml CollectionToSqlXml&lt;T&gt;(IEnumerable&lt;T&gt; TCollection)
   {
    //First convert the collection into a data table, and then convert the data table into SQLXML    return DataTableToSqlXml(CollectionToDataTable(TCollection));
   }
   /// &lt;summary&gt;
   /// Convert collections into data tables   /// &lt;/summary&gt;
   /// <typeparam name="T">Generic Parameters (type of collection member)</typeparam>   /// <param name="TCollection">Generic Collection</param>   /// &lt;returns&gt;&lt;/returns&gt;
   public static DataTable CollectionToDataTable&lt;T&gt;(IEnumerable&lt;T&gt; TCollection)
   {
    //Get the specific type of generic    Type type = typeof(T);
    //Get the public attribute of the type    PropertyInfo[] properties = ();
    //Create a data table, the table name is type name    DataTable table = new DataTable();
    //Convert the public attribute to a table column and then add the table column to the table    foreach (var property in properties)
    {
     //Create a table column with the column name as attribute name and the column data type is the attribute type     DataColumn column = new DataColumn(, );
     //Add table columns to table     (column);
    }
    //Add generic collection elements to the data row    foreach (var item in TCollection)
    {
     //Create table rows with the same schema as table row     DataRow row = ();
     //Read the values ​​of all attribute columns of the element and add the attribute value to the table row according to the attribute name     foreach (var property in properties)
      row[] = (item, null);
     //Add table rows to the table     (row);
    }
    return table;
   }
   /// &lt;summary&gt;
   /// Convert data tables to SQLXML   /// &lt;/summary&gt;
   /// <param name="table">Data table</param>   /// &lt;returns&gt;&lt;/returns&gt;
   public static SqlXml DataTableToSqlXml(DataTable table)
   {
    SqlXml xml;
    //If the table name is empty, set the table name    if (())
      = "TableName";
    //Convert data table to XML    using (var ms = new MemoryStream())
    {
     //Convert the data table to XML format and write it to the memory stream     (ms);
     //Set the memory stream read mark back to the starting point      = 0;
     //Use XmlReader to read the memory stream and create a SqlXml object     xml = new SqlXml((ms));
    }
    return xml;
   }
  }
  /// &lt;summary&gt;
  /// Human (test data class)  /// &lt;/summary&gt;
  class Person
  {
   /// &lt;summary&gt;
   /// Constructor   /// &lt;/summary&gt;
   /// <param name="name">name</param>   /// <param name="age">Age</param>   public Person(string name, int age)
   { Name = name; Age = age; }
   /// &lt;summary&gt;
   /// Name   /// &lt;/summary&gt;
   public string Name { get; set; }
   /// &lt;summary&gt;
   /// age   /// &lt;/summary&gt;
   public int Age { get; set; }
  }
 }
}

Output result:

&lt;DocumentElement&gt;
 &lt;Person&gt;
 &lt;Name&gt;Li Yuanfang&lt;/Name&gt;
 &lt;Age&gt;23&lt;/Age&gt;
 &lt;/Person&gt;
 &lt;Person&gt;
 &lt;Name&gt;Di Renjie&lt;/Name&gt;
 &lt;Age&gt;32&lt;/Age&gt;
 &lt;/Person&gt;
&lt;/DocumentElement&gt;

It mainly uses reflection to read the attributes of generic types, then generate a data table based on the read attributes, and then convert the data table into XML format.

The comments have been written in detail, and I don’t know what else to explain. If this small example can help someone, it would be the best ~

The above is all the content of this article. I hope that the content of this article will help you study or work. I also hope to support me more!