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> /// <returns></returns> public static SqlXml CollectionToSqlXml<T>(IEnumerable<T> TCollection) { //First convert the collection into a data table, and then convert the data table into SQLXML return DataTableToSqlXml(CollectionToDataTable(TCollection)); } /// <summary> /// Convert collections into data tables /// </summary> /// <typeparam name="T">Generic Parameters (type of collection member)</typeparam> /// <param name="TCollection">Generic Collection</param> /// <returns></returns> public static DataTable CollectionToDataTable<T>(IEnumerable<T> 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; } /// <summary> /// Convert data tables to SQLXML /// </summary> /// <param name="table">Data table</param> /// <returns></returns> 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; } } /// <summary> /// Human (test data class) /// </summary> class Person { /// <summary> /// Constructor /// </summary> /// <param name="name">name</param> /// <param name="age">Age</param> public Person(string name, int age) { Name = name; Age = age; } /// <summary> /// Name /// </summary> public string Name { get; set; } /// <summary> /// age /// </summary> public int Age { get; set; } } } }
Output result:
<DocumentElement> <Person> <Name>Li Yuanfang</Name> <Age>23</Age> </Person> <Person> <Name>Di Renjie</Name> <Age>32</Age> </Person> </DocumentElement>
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!