private void ReadProperty(XmlNode node, object instance, ArrayList errors)
{
XmlAttribute nameAttr = node.Attributes["name"];
if (nameAttr == null)
{
errors.Add("Property has no name");
return;
}
PropertyDescriptor prop = TypeDescriptor.GetProperties(instance)[nameAttr.Value];
if (prop == null)
{
errors.Add(string.Format("Property {0} does not exist on {1}", nameAttr.Value, instance.GetType().FullName));
return;
}
// Get the type of this property. We have three options:
// 1. A normal read/write property.
// 2. A "Content" property.
// 3. A collection.
//
bool isContent = prop.Attributes.Contains(DesignerSerializationVisibilityAttribute.Content);
if (isContent)
{
object value = prop.GetValue(instance);
// Handle the case of a content property that is a collection.
//
if (value is IList)
{
foreach(XmlNode child in node.ChildNodes)
{
if (child.Name.Equals("Item"))
{
object item;
XmlAttribute typeAttr = child.Attributes["type"];
if (typeAttr == null)
{
errors.Add("Item has no type attribute");
continue;
}
Type type = Type.GetType(typeAttr.Value);
if (type == null)
{
errors.Add(string.Format("Item type {0} could not be found.", typeAttr.Value));
continue;
}
if (ReadValue(child, TypeDescriptor.GetConverter(type), errors, out item))
{
try
{
((IList)value).Add(item);
}
catch(Exception ex)
{
errors.Add(ex.Message);
}
}
}
else
{
errors.Add(string.Format("Only Item elements are allowed in collections, not {0} elements.", child.Name));
}
}
}
else
{
// Handle the case of a content property that consists of child properties.
//
foreach(XmlNode child in node.ChildNodes)
{
if (child.Name.Equals("Property"))
{
ReadProperty(child, value, errors);
}
else
{
errors.Add(string.Format("Only Property elements are allowed in content properties, not {0} elements.", child.Name));
}
}
}
}
else
{
object value;
if (ReadValue(node, prop.Converter, errors, out value))
{
// ReadValue succeeded. Fill in the property value.
//
try
{
prop.SetValue(instance, value);
}
catch(Exception ex)
{
errors.Add(ex.Message);
}
}
}
}
private object ReadObject(XmlNode node, ArrayList errors)
{
XmlAttribute typeAttr = node.Attributes["type"];
if (typeAttr == null)
{
errors.Add("<Object> tag is missing required type attribute");
return null;
}
Type type = Type.GetType(typeAttr.Value);
if (type == null)
{
errors.Add(string.Format("Type {0} could not be loaded.", typeAttr.Value));
return null;
}
// This can be null if there is no name for the object.
//
XmlAttribute nameAttr = node.Attributes["name"];
// Got an object, now we must process it. Check to see if this tag
// offers a child collection for us to add children to.
//
XmlAttribute childAttr = node.Attributes["children"];
IList childList = null;
if (childAttr != null)
{
PropertyDescriptor childProp = TypeDescriptor.GetProperties(instance)[childAttr.Value];
if (childProp == null)
{
errors.Add(string.Format("The children attribute lists {0} as the child collection but this is not a property on {1}", childAttr.Value, instance.GetType().FullName));
}
else
{
childList = childProp.GetValue(instance) as IList;
if (childList == null)
{
errors.Add(string.Format("The property {0} was found but did not return a valid IList", childProp.Name));
}
}
}
// Now, walk the rest of the tags under this element.
//
foreach(XmlNode childNode in node.ChildNodes)
{
if (childNode.Name.Equals("Object"))
{
// Another object. In this case, create the object, and
// parent it to ours using the children property. If there
// is no children property, bail out now.
if (childAttr == null)
{
errors.Add("Child object found but there is no children attribute");
continue;
}
// no sense doing this if there was an error getting the property. We've already reported the
// error above.
if (childList != null)
{
object childInstance = ReadObject(childNode, errors);
childList.Add(childInstance);
}
}
else if (childNode.Name.Equals("Property"))
{
// A property. Ask the property to parse itself.
//
ReadProperty(childNode, instance, errors);
}
else if (childNode.Name.Equals("Event"))
{
// An event. Ask the event to parse itself.
//
//ReadEvent(childNode, instance, errors);
}
}
return instance;
}