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?
No comments:
Post a Comment