protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["id"] == null)
        {
            Response.StatusCode = 404;
            return;
        }
        else
        {
            int reqId = Convert.ToInt32(Request["id"]);
            SimplePlatformerEntities ents = new SimplePlatformerEntities();
            levels foundLevel             = (from level in ents.levels
                                             where level.id == reqId
                                             select level).FirstOrDefault();

            if (foundLevel == null)
            {
                Response.StatusCode = 404;
                return;
            }
            else
            {
                Response.Clear();
                Response.ContentType = "text/xml";
                Response.AddHeader("content-disposition", "attachment; filename=\"" + foundLevel.name + ".level\"");
                Response.Write(foundLevel.level);
                Response.End();
            }
        }
    }
    protected void SaveLevelButton_Click(object sender, EventArgs e)
    {
        int ySize = Convert.ToInt32(LevelYSizeTextBox.Text);

        XMLToCSharp.Level level = new XMLToCSharp.Level();
        level.Name = LevelNameTextBox.Text;
        level.Blocks = new List<XMLToCSharp.Block>();

        string[] lines = LevelDataTextBox.Text.Split(',');
        for (int row = 0; row < lines.Length; row++)
        {
            for (var col = 0; col < lines[row].Length; col++)
            {
                int realY = ySize - row - 1;
                switch (lines[row][col])
                {
                    case '8':
                        level.Exit = new XMLToCSharp.Exit() { X = col, Y = realY };
                        break;
                    case '4':
                        level.Player = new XMLToCSharp.Player() { X = col, Y = realY };
                        break;
                    case '2':
                        level.Blocks.Add(new XMLToCSharp.Block() { Type = "Gem", X = col, Y = realY });
                        break;
                    case '1':
                        level.Blocks.Add(new XMLToCSharp.Block() { Type = "Ground", X = col, Y = realY });
                        break;
                }
            }
        }

        string resultingFile;
        XmlSerializer serializer = new XmlSerializer(typeof(XMLToCSharp.Level));
        using (StringWriterUtf8 textWriter = new StringWriterUtf8())
        {
            serializer.Serialize(textWriter, level);
            resultingFile = textWriter.ToString();
        }

        SimplePlatformerEntities ents = new SimplePlatformerEntities();
        ents.levels.Add(new levels() { name = level.Name, level = resultingFile });
        ents.SaveChanges();

        Response.Redirect("~/Default.aspx");
    }