Question

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?

Was it helpful?

Solution

You can always look into writing your own XML Content Pipeline Extension. I'm not a huge fan of the Intermediate Serializer because, as you say, it is fairly rough to work with if you have any complex XML.

A simple guide can be found here: http://roy-t.nl/index.php/2010/08/07/xna-xml-contentimporter/

Doing it this way will also allow you to use XPath to get the information you want from your XML, which, in my opinion, is significantly easier to work with.

It may be missing some parts, but you can supplement those using this guide which discusses Content Importer Extensions more completely (and doing maps!): http://rbwhitaker.wikidot.com/content-pipeline-extension-1

If these routes aren't an option, I believe you are correct in your assertion that you will have to write a serializer for the entire map content.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top