title: Monogame - tile map with XML instead of JSON
body: <p>I'm working on the levels for my game so I've built these classes to read my maps:</p>
  
  <p><strong>Tiles.cs:</strong></p>
  
  <pre><code>protected Texture2D texture;
  private Rectangle rectangle;
  public Rectangle Rectangle
  {
      get
      {
          return rectangle;
      }
      protected set
      {
          rectangle = value;
      }
  }
  
  private static ContentManager content;
  public static ContentManager Content
  {
      protected get
      {
          return content;
      }
      set
      {
          content = value;
      }
  }
  
  public void Draw(SpriteBatch spriteBatch)
  {
      spriteBatch.Draw(texture, rectangle, Game1.DefaultColor);
  }
  </code></pre>
  
  <p><strong>CollisionTiles.cs:</strong></p>
  
  <pre><code>public CollisionTiles(int i, Rectangle newRectangle)
  {
      texture = Content.Load&lt;Texture2D&gt;("Graphics/Tiles/tile_" + i);
      Rectangle = newRectangle;
  }
  </code></pre>
  
  <p><strong>Map.cs:</strong></p>
  
  <pre><code>private List&lt;CollisionTiles&gt; collisionTiles;
  public List&lt;CollisionTiles&gt; CollisionTiles
  {
      get
      {
          return collisionTiles;
      }
  }
  
  private int width, height;
  public int Width
  {
      get
      {
          return width;
      }
  }
  
  public int Height
  {
      get
      {
          return height;
      }
  }
  
  public Map()
  {
      collisionTiles = new List&lt;CollisionTiles&gt;();
  }
  
  public void Generate(int[,] map, int size)
  {
      for (int x = 0; x &lt; map.GetLength(1); x++)
      {
          for (int y = 0; y &lt; map.GetLength(0); y++)
          {
              int number = map[y, x];
              if (number &gt; 0)
              {
                  collisionTiles.Add(new CollisionTiles(number, new Rectangle(x * size, y * size, size, size)));
              }
              width = (x + 1) * size;
              height = (y + 1) * size;
          }
      }
  }
  
  public int[,] LoadLevelData(string filename)
  {
      using (StreamReader streamReader = new StreamReader(filename))
      {
          JsonSerializer serializer = new JsonSerializer();
          return (int[,])serializer.Deserialize(streamReader, typeof(int[,]));
      }
  }
  
  public void Draw(SpriteBatch spriteBatch)
  {
      foreach (CollisionTiles tile in collisionTiles)
      {
          tile.Draw(spriteBatch);
      }
  }
  </code></pre>
  
  <p><strong>level_1.json:</strong></p>
  
  <pre><code>[
      [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
      [2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1],
      [2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 2, 2],
      [2, 2, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 1, 0, 0, 0, 0, 2, 2],
      [2, 2, 0, 0, 0, 0, 0, 0, 1, 2, 2, 2, 2, 2, 2, 2, 1, 0, 0, 0, 2, 2],
      [2, 0, 0, 0, 0, 0, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 2, 2],
      [2, 0, 0, 0, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
      [2, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
      [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
      [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
  ]
  </code></pre>
  
  <p>I have the Newtonsoft.Json package added to my project. But since Monogame does not natively support JSON, rather its <em>Content Pipeline</em> supports XML. If I were to ship my game with the current level loading method, I would be forced to ship it with the levels as JSON files and not binary XNBs. That would enable users to edit the levels however they like and I don't want that.</p>
  
  <p>So how can this code be converted to load XML levels instead of JSON? What approach would you take to deal with the problem? I've never used Monogame's/XNA's XML support in my projects before but using it for my levels would be a nice start. All help is appreciated.</p>
  
  <p><strong>Edit:</strong></p>
  
  <p><strong>Usage</strong>:</p>
  
  <pre><code>int defaultTileSize, level_number;
  Map map;
  
  // constructor:
  defaultTileSize = 64;
  level_number = 1;
  
  // LoadContent()
  int[,] levelData = map.LoadLevelData("level_" + level_number + ".json");
  map.Generate(levelData, defaultTileSize);
  
  // Draw()
  map.Draw(spriteBatch);
  </code></pre>