Dynamic XML Creation Using Reflection

Introduction

It's usual to create a XML Document using some information created dynamically. Usually we have a type that have one or more properties (or fields) were stored all information. Creating a simple XML accessing to all properties or fields is a loss of time. Eventually, some types have a specific XML element. In those cases, we must manually generate that element, but only those elements. But using reflection we can create xml by accessing properties of an object.

Reflection is mechanism of discovering class information solely at run time. Using reflection you would see all the properties, functions, events, constructor of an object.

About Refection

System.Reflection.Assembly

It defines an assembly which is a reusable, version able and self describing building block of a Common Language Run time application. I will discuss here only the functions that I have used in my code.

objAssembly = System.Reflection.Assembly.LoadFrom(str);

LoadFrom function takes the path of the assembly to load and loads it and returns that Assembly.

After loading the assembly we want to get all the types in that assembly.This class provides a function GetTypes() (It returns an array of System.Type objects in that assembly) which will do that for us and that line for the code is.

arOfTypes = objAssembly.GetTypes();

Now having touched System.Type class,Let us talk about it.

System.Type:

This class represents type declarations.(class types, interface types, array types, value types and enumeration types.)

It is the root of all Reflection operations.It is the primary means by which we access metadata and it acts as a Gateway to Reflection API.It provides methods for obtaining information about a Type declaration, such as the constructors, properties, methods and Events.

Extracting Constructor Information:

ConstructorInfo[] ctrInfo=t.GetConstructors();

It is an object of type System.Type. This method returns an array of objects of type ConstructorInfo. This calss is part of System.Reflection Name space.

Extracting Properties Information:

PropertyInfo[] pInfo = t.GetProperties() ;

//It is an object of type System.Type. This method returns an array of objects of type PropertyInfo. This calss is part of System.Reflection Name space.

Extracting Method Information:

MethodInfo[] mInfo=t.GetMethods();

Extracting Event Information:

EventInfo[] eInfo=t.GetEvents();

Once I get all this info,I am presenting it in a tree view control.This should fairly give a good idea of getting information about a Type.

Once you know all the information about a class, what do you want to do? Probably you want to create an instance of that class and execute one of its methods. I have created two classes MyClass1 and MyClass2 as part of this name space and in the click event of more reflection button I have created an instance of each of these classes and invoked its methods. I have put message boxes so that we would know that they are getting executed. Let us look at this code.

Type t= Type.GetType("MyReflection.MyClass1");

I have passed the name of the class to static method GetType of Type class which would return me a type Object of type MyClass1.Once you get Type object, as mentioned before you could do lot of things with it. Let us look at Activator class and see what we can do with this class and Type class.

Example

In this simple case, we have a public class were allocated the information. The class looks like following:

public class Identification
{
    private string _name = null;
    private string _address = null;
    public string Name
    {
        get
        {
            return _name;
        }
        set
        {
            _name = value;
        }
    }

    public string Address
    {
        get
        {
            return _address;
        }
        set
        {
            _address = value;
        }
    }
}

Next we create an instance of Identification class.

Identification identification = new Identification();

identification.Name = "Edgar";

identification.Address = "Lisbon";



Finally create XML Document using reflection to access properties of our Identification instance.
 

XmlDocument xmlDoc = new XmlDocument();
XmlDeclaration xmlDec = xmlDoc.CreateXmlDeclaration("1.0", "utf-8",     String.Empty);
xmlDoc.PrependChild(xmlDec);
XmlElement elemRoot = xmlDoc.CreateElement("Identification");
xmlDoc.AppendChild(elemRoot);
XmlElement elem = null;
Type idType = identification.GetType();
foreach (PropertyInfo pInfo in idType.GetProperties())
{
    object o = pInfo.GetValue(identification, null);
    elem = xmlDoc.CreateElement(pInfo.Name);
    elem.InnerText = o.ToString();
    elemRoot.AppendChild(elem);
}

Conclusion

In this article we have seen that how reflection is helpful for creating xml file.

Is this the Work Around or Best Solution?

Yes this is Best Solution.


Similar Articles