I'm making a game with XNA, and trying to write all level information into an XML by using the IntermediateSerializer.
So far I had no problem doing this, but the XML have a little too many tags, and I want to make it more efficient.
So for example, I have this:
<Asset Type="Data:MapContent">
<Zone>Jungle</Zone>
<MapID>Jungle01</MapID>
<Size>42 40</Size>
<Objects>
<MapObject>
<Tag>Tree</Tag>
<Position>12.10001 1.41174912 9.433376</Position>
</MapObject>
<MapObject>
...
Which I would like to turn to this:
<Asset Type="Data:MapContent">
<Zone>Jungle</Zone>
<MapID>Jungle01</MapID>
<Size>42 40</Size>
<Objects>
<MapObject Tag="Tree">12.10001 1.41174912 9.433376</MapObject>
...
This is the serializer I tried to make for MapObject
[ContentTypeSerializer]
class MapObjectSerializer : ContentTypeSerializer<MapObject>
{
protected override void Serialize(IntermediateWriter output, MapObject value, ContentSerializerAttribute format)
{
output.Xml.WriteAttributeString("Tag", value.Tag);
output.WriteObject(value.Position, new ContentSerializerAttribute() { ElementName = "Position", FlattenContent = true });
}
protected override MapObject Deserialize(IntermediateReader input, ContentSerializerAttribute format, MapObject existingInstance)
{
existingInstance.Tag = input.Xml.GetAttribute("Tag");
string[] pos = input.Xml.ReadElementString().Split(' ');
existingInstance.Position = new Vector3(float.Parse(pos[0]), float.Parse(pos[1]), float.Parse(pos[2]));
return existingInstance;
}
}
The Serialize function works, I get the exact XML I wanted, but the Deserialize method doesn't. It looks like when the method is called, the XML parser is already in this position:
v
<MapObject Tag="Tree">12.10001 1.41174912 9.433376</MapObject>
Is there any way I could retrieve the "Tag" attribute without having to write a Serializer for the whole MapContent?
Or else, any other way to make the XML efficient, but without using attributes?