1
\$\begingroup\$

I want my xml files to be game ready on save in my winforms. Right now I am trying to serialize a simple object like this and it is accessing the XML file, but not outputting anything. Am I missing something? I have tried adding [Serializable()] to myTest and that did not work either.

using System;
using System.IO;
using System.Windows.Forms;

namespace WinformXMLIntermediateSerializer
{
 public partial class Form1 : Form
 {
    private const string filePath = "test.xml";
    private myTest mytest = new myTest();
    public Form1()
    {
        InitializeComponent();
    }

    private void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            if (File.Exists(filePath)) {
                XnaSerializer.Serialize(filePath, mytest);
                label1.Text = "File serialized...";
            }
        }
        catch(Exception Ex) {
            MessageBox.Show(Ex.ToString());
        }
    }
  }
}

Serialize Class:

using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate;
using System.IO;
using System.Xml;

static class XnaSerializer
{
 public static void Serialize<T>(string filename, T data)
 {
    XmlWriterSettings settings = new XmlWriterSettings();
    settings.Indent = true;
    using (XmlWriter writer = XmlWriter.Create(filename, settings))
    {
        IntermediateSerializer.Serialize<T>(writer, data, null);
    }
 }   
}

Object to be serialized:

using Microsoft.Xna.Framework;

public class myTest
{
 private string name;
 public string Name
 {
    get { return name; }
    set { name = value; }
}

private Point tilesize;
public Point TileSize
{
    get { return tilesize; }
    set { tilesize = value; }
}

 public myTest() {
    Name = "test name";
    TileSize = new Point(32, 32);
 }
}
\$\endgroup\$
  • \$\begingroup\$ Interestingly enough, I downloaded the monogame source and added it as a project, referenced it and this worked perfectly. I got the Pipeline.dll that is not working from nuget, so maybe there was a problem there? I only referenced what I needed to in monogame once I added the project. I will continue to investigate this. \$\endgroup\$ – Svetoslav Jul 12 '16 at 20:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.