With the game I'm currently working, I planned to store levels in XML format (With the default levels stored as resources, but with user-creatable levels in XML files). I've decided to use .NET's own XmlSerializer class, however I've run across a problem.
I have a class, Vector, that I want to serialize into the form "X,Y". This works fine if it's stored as an element, but I'd rather have it as an attribute, as
<Cannon LaunchVector="14,8" />
is more compact and looks nicer (Not to mention, makes more sense) than:
<Cannon><LaunchVector>14,8</LaunchVector></Cannon>
When I try to compile with this code:
private Vector launchVector;
/// <summary>
/// Gets and sets the vector that the ball is automatically
/// launched along. Only used in the 'menu' level.
/// </summary>
[XmlElement(ElementName = "LaunchVector")]
public Vector LaunchVector
{
get { return launchVector; }
set { launchVector = value; }
}
it all compiles, and runs, fine. However, if I try to use an attribute:
[XmlAttribute(AttributeName = "LaunchVector")]
it throws an exception at runtime: "There was an error reflecting Cannons.LevelData" at this line:
XmlSerializer xmlSerializer = new XmlSerializer(typeof(LevelData));
I don't want to over-paste code, so I chopped it down a lot, but hopefully I haven't missed anything important. Hope some can help,
Thanks - Crehl.