using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using System.Reflection;
using System.Xml;
using System.Xml.Linq;
namespace DataDrivenExample
{
class XMLParse
{
///
/// Returns the value of an attribute of an XElement, or "" if no such
/// attribute exists
///
/// Element to get attribute from
/// Name of the attribute to extract
/// Value of the attribute, or "" if it doesn't exist
protected static string GetAttributeValue(XElement elem, String AttributeName)
{
foreach (XAttribute attr in elem.Attributes())
{
if (attr.Name.ToString() == AttributeName)
{
return attr.Value;
}
}
return "";
}
///
/// Adds a List<Vector2> valued property to an object, based on
/// an XML node
///
/// XML element to read name / value of property from
/// Object to set proprerty on
protected static void AddVector2ListToClassInstance(XElement elem, Object obj)
{
List vectorList = new List();
foreach (XElement pathPoint in elem.Elements())
{
Vector2 nextElem = new Vector2(float.Parse(pathPoint.Element("x").Value),
float.Parse(pathPoint.Element("y").Value));
vectorList.Add(nextElem);
}
PropertyInfo propertyInfo = obj.GetType().GetProperty(elem.Name.ToString());
propertyInfo.SetValue(obj, vectorList, null);
}
///
/// Adds a Vector2 valued property to an object, based on
/// an XML node
///
/// XML element to read name / value of property from
/// Object to set proprerty on
protected static void AddVector2ToClassInstance(XElement elem, Object obj)
{
PropertyInfo propertyInfo = obj.GetType().GetProperty(elem.Name.ToString());
Vector2 valueToSet = new Vector2(float.Parse(elem.Element("x").Value),
float.Parse(elem.Element("y").Value));
propertyInfo.SetValue(obj, valueToSet, null);
}
///
/// Adds a float-valued property to an object, based on
/// an XML node
///
/// XML element to read name / value of property from
/// Object to set proprerty on
protected static void AddFloatToClassInstance(XElement elem, Object obj)
{
PropertyInfo propertyInfo = obj.GetType().GetProperty(elem.Name.ToString());
propertyInfo.SetValue(obj, float.Parse(elem.Value), null);
}
///
/// Adds an int-valued property to an object, based on
/// an XML node
///
/// XML element to read name / value of property from
/// Object to set proprerty on
protected static void AddIntToClassInstance(XElement elem, Object obj)
{
PropertyInfo propertyInfo = obj.GetType().GetProperty(elem.Name.ToString());
propertyInfo.SetValue(obj, int.Parse(elem.Value), null);
}
///
/// Adds a string-valued property to an object, based on
/// an XML node
///
/// XML element to read name / value of property from
/// Object to set proprerty on
protected static void AddStringToClassInstance(XElement elem, Object obj)
{
PropertyInfo propertyInfo = obj.GetType().GetProperty(elem.Name.ToString());
propertyInfo.SetValue(obj, elem.Value.Trim(), null);
}
///
/// Adds a bool-valued property to an object, based on
/// an XML node
///
/// XML element to read name / value of property from
/// Object to set proprerty on
protected static void AddBoolToClassInstance(XElement elem, Object obj)
{
PropertyInfo propertyInfo = obj.GetType().GetProperty(elem.Name.ToString());
string value = elem.Value.Trim().ToLower();
bool boolVal = value == "true" || value == "t" || value == "1";
propertyInfo.SetValue(obj, boolVal, null);
}
///
/// Adds a property value to an object, based on an XML node. Use the
/// 'type' attribute of the XML node to determine what type the element
/// we are adding is -- float, Vector2, string, etc.
///
/// XML element to read name / value of property from
/// Object to set proprerty on
public static void AddValueToClassInstance(XElement elem, Object obj)
{
string type = GetAttributeValue(elem, "type").ToLower();
if (type == "float")
{
AddFloatToClassInstance(elem, obj);
}
else if (type == "int")
{
AddIntToClassInstance(elem, obj);
}
else if (type == "vector2")
{
AddVector2ToClassInstance(elem, obj);
}
else if (type == "rectangle")
{
AddRectangleToClassInstance(elem, obj);
}
else if (type == "string")
{
AddStringToClassInstance(elem, obj);
}
else if (type == "bool")
{
AddBoolToClassInstance(elem, obj);
}
else if (type == "vector2list")
{
AddVector2ListToClassInstance(elem, obj);
}
else
{
throw new FormatException("Unknown Type attribute " + type + " in XML file");
}
}
private static void AddRectangleToClassInstance(XElement elem, object obj)
{
PropertyInfo propertyInfo = obj.GetType().GetProperty(elem.Name.ToString());
Rectangle valueToSet = new Rectangle(int.Parse(elem.Element("x").Value),
int.Parse(elem.Element("y").Value),
int.Parse(elem.Element("w").Value),
int.Parse(elem.Element("h").Value));
propertyInfo.SetValue(obj, valueToSet, null);
}
}
}