Matonster
Matonster

Reputation: 3

Strange XmlSerializer behavior

So i'm having some trouble deserializing an XML file.
I'm using the following struct.

[Serializable]
public struct GraphicsOptions
{
    public int Height;
    public int Width;
    public bool Fullscreen;
    public bool AntiAliasing;
    public int ClickResCount;
}

And the following code to Create,

public void CreateData()
{
    graphicsOptions.Height = graphics.PreferredBackBufferHeight;
    graphicsOptions.Width = graphics.PreferredBackBufferWidth;
    graphicsOptions.Fullscreen = graphics.IsFullScreen;
    graphicsOptions.AntiAliasing = graphics.PreferMultiSampling;
    graphicsOptions.ClickResCount = 1;
    dataStream = File.Create(SavegamePath);
    XmlSerializer serializer = new XmlSerializer(typeof(GraphicsOptions));
    serializer.Serialize(dataStream, graphicsOptions);
    dataStream.Close();
}

Change and

private void ApplyChanges()
{
    graphicsOptions.Height = graphics.PreferredBackBufferHeight;
    graphicsOptions.Width = graphics.PreferredBackBufferWidth;
    graphicsOptions.Fullscreen = graphics.IsFullScreen;
    graphicsOptions.AntiAliasing = graphics.PreferMultiSampling;
    graphicsOptions.ClickResCount = clickCountResolution;
    dataStream = File.Open(SavegamePath, FileMode.Open);
    XmlSerializer serializer = new XmlSerializer(typeof(GraphicsOptions));
    serializer.Serialize(dataStream, graphicsOptions);
    dataStream.Close();
}

Load the XML file

public void LoadData()
{
    dataStream = File.Open(SavegamePath, FileMode.Open);
    XmlSerializer serializer = new XmlSerializer(typeof(GraphicsOptions));
    graphicsOptions = (GraphicsOptions)serializer.Deserialize(dataStream);
    dataStream.Close();
 }

Pretty standard stuff, except after the third time I apply the changes it decides to add 2 characters at the end of the file: "s>".

I have no idea why it does this, but it makes the XML practically useless because I can't load the information into my struct.
Visual Studio is giving me an InvalidOperationException (I understand why it does this).

Any advice or ideas on how to prevent this from occuring or how to simply delete the 2 characters if it catches the exception?

Upvotes: 0

Views: 88

Answers (2)

MNie
MNie

Reputation: 1367

It happens because your new file is short than it was previously. You should use FileMode.Create instead of Open/OpenOrCreate

So Your save function should look like this::

private void ApplyChanges()
{
    graphicsOptions.Height = graphics.PreferredBackBufferHeight;
    graphicsOptions.Width = graphics.PreferredBackBufferWidth;
    graphicsOptions.Fullscreen = graphics.IsFullScreen;
    graphicsOptions.AntiAliasing = graphics.PreferMultiSampling;
    graphicsOptions.ClickResCount = clickCountResolution;
    using(dataStream = File.Open(SavegamePath, FileMode.Create))
    {
        XmlSerializer serializer = new XmlSerializer(typeof(GraphicsOptions));
        serializer.Serialize(dataStream, graphicsOptions);
    }
}

Upvotes: 1

Lucas Damiani
Lucas Damiani

Reputation: 77

Change the ApplyChanges() method to use another FileMode, like FileMode.Create. You can find the correct method below.

 private void ApplyChanges()
 {
    graphicsOptions.Height = graphics.PreferredBackBufferHeight;
    graphicsOptions.Width = graphics.PreferredBackBufferWidth;
    graphicsOptions.Fullscreen = graphics.IsFullScreen;
    graphicsOptions.AntiAliasing = graphics.PreferMultiSampling;
    graphicsOptions.ClickResCount = clickCountResolution;
    dataStream = File.Open(SavegamePath, FileMode.Create); // You can use FileMode.Truncate as well.
    XmlSerializer serializer = new XmlSerializer(typeof(GraphicsOptions));
    serializer.Serialize(dataStream, graphicsOptions);
    dataStream.Close();
  }

See https://msdn.microsoft.com/en-us/library/system.io.filemode(v=vs.110).aspx

Upvotes: 0

Related Questions