Reflection - Transform a dotnet component to XML

  • Thread starter Thread starter Ahab Guirguis
  • Start date Start date
A

Ahab Guirguis

hello,

I receive an error with the recursive call......
Can any one see a problem with this approach....
Thanks in advance....


string xml = ToXmlProperties(new TextBox()
FwTextBoxXml.Text = xml;
//--------------------------------------------------------------------------
------
public string ToXmlProperties(Object obj)
{
string xml = String.Empty;
Type t = obj.GetType();
string propertyValue = String.Empty;
xml = "<properties>";
PropertyInfo [] pi = t.GetProperties(BindingFlags.Instance |
BindingFlags.NonPublic | BindingFlags.Public);
try
{
foreach (PropertyInfo p in pi)
{
xml += "<property>";
xml += "<name>" + p.Name.ToString() + "</name>";
xml += "<PropertyType>" + p.PropertyType.ToString() + "</PropertyType>";
xml += "<IsClass>" + p.PropertyType.IsClass.ToString() + "</IsClass>";
if ((p.PropertyType.IsClass))
{
object objProperty = p.GetValue(obj, Type.EmptyTypes);
propertyValue = ToXmlProperties(objProperty);
}
else
{
propertyValue = p.GetValue(obj, Type.EmptyTypes).ToString();
}
xml += "<value>" + propertyValue + "</value>";
xml += "</property>";
}
}
catch (System.NullReferenceException ex) {}
xml += "</properties>";
return xml;
}
//--------------------------------------------------------------------------
------
 
Ahab,
I receive an error with the recursive call......

What error is that?

One thing you should probably check for is if objProperty is null.

Also, you should consider using a StringBuilder to build the XML
string.



Mattias
 
Back
Top