Game Development Reference
In-Depth Information
Now that we have a simpler XML document, let's mark up our original
CharacterData class from Listing 12.3 using the XML serializer attributes. The
full list of attributes can be found in a document entitled “Controlling XML Seri-
alization Using Attributes” on Microsoft's MSDN site. For our purposes, we only
need two:
1. [XmlElement] : controls the mapping between a field and the element name
in the document;
2. [XmlRoot] : used to control mapping between a class and the root element
name of the document.
Listing 12.8 shows our CharacterData class from Listing 12.3 with attributes
added.
public enum CharacterClass
{
Standard,
Armored
}
public class CharacterData
{
[XmlElement("Character")]
public string character;
[XmlElement("Description")]
public string description;
[XmlElement("HitPoints")]
public float hitPoints;
[XmlElement("MoveSpeed")]
public float moveSpeed;
[XmlElement("AttackPower")]
public float attackPower;
[XmlElement("Class")]
public CharacterClass characterClass;
[XmlElement("Thumbnail")]
public string thumbnail;
public override string ToString ()
{
StringBuilder sb = new StringBuilder();
sb.AppendFormat("{0} ({1})\n", character,
characterClass.ToString());
sb.Append( ￿ \t ￿ );
sb.AppendLine(description);
sb.AppendFormat("\thp: {0:0}\tms: {1:0}\tap: {2:0}\n",
hitPoints, moveSpeed, attackPower);
sb.AppendFormat("\tthumbnail: {0}\n", thumbnail);
return sb.ToString();
Listing 12.8. System.Xml.Serialization attribute mark-up (autoxml.cs).
Search WWH ::




Custom Search