method of converting a generic collection of any type into an XML format string using LINQ to XML and Reflection in C # _c# tutorial

Source: Internet
Author: User
Tags reflection string format

At work, if you need to deal with XML, you will inevitably encounter situations where you need to convert a collection of types into XML. The previous approach was clumsy, requiring different types to write a converted function. But then, after touching the reflection, you know that you can use reflection to read all the members of a type, which means you can create more generic methods for different types. This example does this by using reflection to read all the attributes of a type and then to convert the attribute to an attribute or child element of an XML element. The following comments are more complete, the words do not say more, there is a need to see the code!

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Xml.Linq;
Using System.Reflection;  Namespace Genericcollectiontoxml {class Program {static void Main (string[] args) {var persons = new[]{New
   Person () {name= ' Yuanfang ', age=23}, new Person () {name= ' Detective Dee ', Age=32}};
  Console.WriteLine (collectiontoxml (persons)); ///<summary>///Collection converted to datasheet///</summary>///<typeparam name= "T" > generic parameter (Type of collection member) </typeparam&
  Gt <param name= "tcollection" > Generic set </param>///<returns> collection XML format string </returns> public static S Tring collectiontoxml<t> (ienumerable<t> tcollection) {//define element array var elements = new List<xelement&gt
   ;();
    Adds the elements in the collection to the element array foreach (var item in tcollection) {//Gets the specific type of the generic types = typeof (T);
    Defines an array of attributes, Xobject is the base class of XAttribute and xelement var attributes = new list<xobject> (); Gets all of the properties of the type and adds the properties and values to the property array foreach (Var The property in type. GetProperties ())//Gets the property name and property value, added to the property array (also can be added as a child element to the property array, just change the XAttribute to XElement) attributes. ADD (New XAttribute property. Name, property.
    GetValue (item, NULL)); Adds a property array to the element elements. ADD (The New XElement (type).
   Name, attributes)); ///initializes the root element and takes the element array as a child element of the root element, returning the string format (XML) of the root element return new XElement ("root", elements).
  ToString (); ///<summary>///Human (test data Class)///</summary> class Person {///<summary>///name///
   </summary> public string Name {get; set;}
  <summary>///age///</summary> public int ages {get; set;} }
 }
}

To export a property as a property:

<Root>
 <person name= "Yuanfang" age= "/> <person name="
 Detective Dee "age="/> "
</Root>

To export a property as a child element:

<Root>
 <Person>
 <Name> yuanfang </Name>
 <Age>23</Age>
 </ person>
 <Person>
 <Name> Detective Dee </Name>
 <Age>32</Age>
 </ Person>
</Root>

The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring some help, but also hope that a lot of support cloud Habitat community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.