List Of Objects To XMLNodes

// June 18th, 2010 // Useful Code

This is an extension to create an XML representation of an array of classes or structures. Currently it only supports basic types which are not nested. This could be adapted to accomodate nested classes with fairly minimal changes.

//Code


/// <summary>
/// RETURNS AN XML NODE WHICH CONTAINS ALL THE PROPERTIES FOR A LIST OF BASIC TYPES
/// </summary>
/// <typeparam name="t">PARAMETER TYPE WITHIN THE LIST</typeparam>
/// <param name="obj">INCOMING LIST TO ITERATE THROUGH</param>
/// <param name="ElementName">ROOT ELEMENT NAME TO CREATE</param>
/// <returns>AN XML NODE WHICH CONTAINS ALL THE PROPERTIES FOR A LIST OF BASIC TYPES</returns>
public static XElement CreateXMLNode<t>(this IEnumerable<t> obj, string ElementName)
{
    //NODE WHICH WILL BE RETURNED TO THE CALLING FUNCTION
    XElement tmpReturn = new XElement(ElementName);

    //LOOP THROUGH ALL THE INTERNAL OBJECTS TO ADD NODES TO THE ROOT
    foreach (object tmpObj in obj)
    {
        //TEMP NODE REPRESENTING THE PROPERTY ELEMENT
        XElement tmpNode = new XElement(tmpObj.GetType().Name.ToString());

        //LOOP THROUGH ALL THE FIELDS WHICH ARE PUBLIC
        foreach (FieldInfo tmpField in tmpObj.GetType().GetFields())
        {
            XElement tmpElement = new XElement(tmpField.Name);

            //IF THE FIELD IS PUBLIC THEN ADD THE FIELD TO THE NODE
            if (tmpField.IsPublic)
            {
                tmpElement.Value = tmpField.GetValue(tmpObj).ToString();
                tmpNode.Add(tmpElement);
            }
        }

        //LOOP THROUGH ALL THE PROPERTIES WHICH ARE PUBLIC
        foreach (PropertyInfo tmpProp in tmpObj.GetType().GetProperties())
        {
            XElement tmpElement = new XElement(tmpProp.Name);

            //ADD THE ELEMENT TO THE NODE
            tmpElement.Value = tmpProp.GetValue(tmpObj, null).ToString();
            tmpNode.Add(tmpElement);
        }

        //RETURN THE ROOT NODE TO THE CALLING FUNCTION
        tmpReturn.Add(tmpNode);
    }

    return tmpReturn;
}

//Usage


class Program
{
    static void Main(string[] args)
    {
        //CREATE A LIST OF CLASSES
        List<MyClass> tmpList1 = new List<MyClass>();
        tmpList1.Add(new MyClass() { EmployeeName = "Emp 1", Address = "Add 1", Salary = 100000 });
        tmpList1.Add(new MyClass() { EmployeeName = "Emp 2", Address = "Add 2", Salary = 50000 });
        tmpList1.Add(new MyClass() { EmployeeName = "Emp 3", Address = "Add 3", Salary = 65000 });
        tmpList1.Add(new MyClass() { EmployeeName = "Emp 4", Address = "Add 4", Salary = 80000 });

        //CREATE A LIST OF STRUCTS
        List<MyStruct> tmpList2 = new List<MyStruct>();
        tmpList2.Add(new MyStruct() { EmployeeName = "Emp 1", Address = "Add 1", Salary = 100000 });
        tmpList2.Add(new MyStruct() { EmployeeName = "Emp 2", Address = "Add 2", Salary = 50000 });
        tmpList2.Add(new MyStruct() { EmployeeName = "Emp 3", Address = "Add 3", Salary = 65000 });
        tmpList2.Add(new MyStruct() { EmployeeName = "Emp 4", Address = "Add 4", Salary = 80000 });


        //CREATE XML NODE CONTAINING THE LIST OF OBJECTS CREATED ABOVE
        XElement root1 = tmpList1.CreateXMLNode("MyList_Of_Classes");
        XElement root2 = tmpList2.CreateXMLNode("MyList_Of_Structs");
    }
}
    
//BASIC CLASS CONTAINING PUBLIC PROPERTIES
class MyClass
{
    public string EmployeeName { get; set; }
    public string Address { get; set; }
    public double Salary { get; set; }
}

//BASIC STRUCTURE CONTAINING PUBLIC FIELDS
struct MyStruct
{
    public string EmployeeName;
    public string Address;
    public double Salary;
}

//Output


<MyList_Of_Classes>
  <MyClass>
    <EmployeeName>Emp 1</EmployeeName>
    <Address>Add 1</Address>
    <Salary>100000</Salary>
  </MyClass>
  <MyClass>
    <EmployeeName>Emp 2</EmployeeName>
    <Address>Add 2</Address>
    <Salary>50000</Salary>
  </MyClass>
  <MyClass>
    <EmployeeName>Emp 3</EmployeeName>
    <Address>Add 3</Address>
    <Salary>65000</Salary>
  </MyClass>
  <MyClass>
    <EmployeeName>Emp 4</EmployeeName>
    <Address>Add 4</Address>
    <Salary>80000</Salary>
  </MyClass>
</MyList_Of_Classes>



<MyList_Of_Structs>
  <MyStruct>
    <EmployeeName>Emp 1</EmployeeName>
    <Address>Add 1</Address>
    <Salary>100000</Salary>
  </MyStruct>
  <MyStruct>
    <EmployeeName>Emp 2</EmployeeName>
    <Address>Add 2</Address>
    <Salary>50000</Salary>
  </MyStruct>
  <MyStruct>
    <EmployeeName>Emp 3</EmployeeName>
    <Address>Add 3</Address>
    <Salary>65000</Salary>
  </MyStruct>
  <MyStruct>
    <EmployeeName>Emp 4</EmployeeName>
    <Address>Add 4</Address>
    <Salary>80000</Salary>
  </MyStruct>
</MyList_Of_Structs>
Be Sociable, Share!
  • RSS
  • Twitter
  • Facebook
  • LinkedIn
  • DeviantArt