Reflection - Transform a dotnet component to XML

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;
}
//--------------------------------------------------------------------------
------
 
M

Mattias Sjögren

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top