Pages

Monday, September 12, 2005

Convert Custom Objects in XML Data Using Reflection

//Created By Kaushal
public string ConvertToXML()
{


//output will used to store parent object's xml representation

StringBuilder output = new StringBuilder();

//output will used to store child object's xml representation

StringBuilder refOutput = new StringBuilder();

//I am considering Object Node is Root Node..


output.Append("");

//Getting Count of parent Objects

for (int i=0;i<=this.Count-1;i++)
{

//Getting Properties Info using Reflection

PropertyInfo[] objFields = this.List[i].GetType().GetProperties();

//Adding Reflected Object Type in to XML String as Parent Node of an Entity as Node Name

output.Append("<" + objFields[0].ReflectedType.Name.ToString() + ">");

//Appending Data as seprate Attribute in the node

output.Append("<" + objFields[0].ReflectedType.Name.ToString());

// Accessing each property

foreach(PropertyInfo prop in objFields)
{

//Checking that the current property is Readable or not

if (prop.CanRead)
{

//If yes then checking the return type of the property. If its Value Type or Normal String I am handling it in the ELSE part.

if ((!prop.PropertyType.IsValueType) && (prop.PropertyType !=typeof(System.String)) && (prop.PropertyType !=typeof(Framework.CError)) )

{
//Getting Value from Reflected Object
object oCustomObject = prop.GetValue(this[i] as object,null);

//Appending object's value into XML

if (null!=oCustomObject)
{
//if object type is ref. type then it will go for recursion call in the next private function.

refOutput.Append(ConvertToXML(oCustomObject));
}
else
{
//Getting data of property in case of value type or String
Object strObjectString = prop.GetValue(this.List[i] as object,null);

if (null!=strObjectString)
{
output.Append(" " + prop.Name.ToString() + "='");
output.Append(strObjectString.ToString()+ "'");
}
}
}
}
output.Append("/>");
output.Append(refOutput.ToString());
refOutput.Remove(0,refOutput.Length);
output.Append(" output = output.Replace("Key","ID");
}
output.Append("");
return output.ToString();
}

//Created By Kaushal
private string ConvertToXML(object oUserObject)
{

StringBuilder output = new StringBuilder();
StringBuilder refOutput = new StringBuilder();
Type objectType = oUserObject.GetType().BaseType;
int iCount = objectType==typeof(System.Object)? 1 : (oUserObject as EntityCollectionBase).Count;
output.Append(objectType==typeof(System.Object)? "<" + oUserObject.GetType().Name + ">" : "");

for (int i=0;i<=iCount-1;i++)
{
PropertyInfo[] objFields = objectType==typeof(System.Object)? oUserObject.GetType().GetProperties() : (oUserObject as EntityCollectionBase)[i].GetType().GetProperties();
Object oTmpObject = objectType==typeof(System.Object)? oUserObject : (oUserObject as EntityCollectionBase)[i] as object;
output.Append("<" + objFields[0].ReflectedType.Name.ToString() + ">");
output.Append("<" + objFields[0].ReflectedType.Name.ToString());

foreach(PropertyInfo prop in objFields)
{
if (prop.CanRead)
{
if ((!prop.PropertyType.IsValueType) && (prop.PropertyType !=typeof(System.String)) && (prop.PropertyType !=typeof(Framework.CError)) )
{
object oCustomObject = prop.GetValue(oTmpObject as object,null) as object;
if (null!=oCustomObject)
//Making Nested Calls
refOutput.Append(ConvertToXML(oCustomObject));
}
else
{
Object strObjectString = prop.GetValue(oTmpObject,null);
if (null!=strObjectString)
{
output.Append(" " + prop.Name.ToString() + "='");
output.Append(strObjectString.ToString()+ "'");
}
}
}
}
output.Append("/>");
output.Append(refOutput.ToString());
refOutput.Remove(0,refOutput.Length);
output.Append("
output = output.Replace("Key","ID");
}

output.Append(objectType==typeof(System.Object)? "return output.ToString();

}


4 More Details Mail me @ mail2kaushal@gmail.com