Jump to content
  • Advertisement
Sign in to follow this  
Spa8nky

[C#] Winforms XML serializer and deserializer causing problems.

This topic is 2660 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm using the following to serialize (save) my data as an XML file:



LevelSettings levelSettings = game.Level.Write();

Stream stream = null;

SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.CheckFileExists = false;
saveFileDialog.CheckPathExists = true;
saveFileDialog.InitialDirectory = "\\";
saveFileDialog.Filter = "XML files (*.xml)|*.xml";
saveFileDialog.FilterIndex = 2;
saveFileDialog.RestoreDirectory = true;

if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
try
{
if ((stream = saveFileDialog.OpenFile()) != null)
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.OmitXmlDeclaration = true;

using (XmlWriter xmlWriter = XmlWriter.Create(stream, settings))
{
XmlSerializer serializer = new XmlSerializer(typeof(LevelSettings)); // Can this stored elsewhere?
serializer.Serialize(xmlWriter, levelSettings);
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not save file to the disk. Original error: " + ex.Message);
}
}


and the following code to deserialize (load) the data from an XML file:



Stream stream = null;

OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.CheckFileExists = true;
openFileDialog.CheckPathExists = true;
openFileDialog.InitialDirectory = "\\";
openFileDialog.Filter = "XML files (*.xml)|*.xml";
openFileDialog.FilterIndex = 2;
openFileDialog.RestoreDirectory = true;

if (openFileDialog.ShowDialog() == DialogResult.OK)
{
try
{
if ((stream = openFileDialog.OpenFile()) != null)
{
// XML READER is only used here, replace it with StreamReader if required
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreComments = true;

using (XmlReader reader = XmlReader.Create(stream, settings))
{
// No object should be selected when loading a new level
//objectSelected = null;

XmlSerializer serializer = new XmlSerializer(typeof(LevelSettings)); // Can this stored elsewhere?
LevelSettings levelSettings = serializer.Deserialize(stream) as LevelSettings;

game.Level.Read(levelSettings);
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}


The problem is that when saving a file and trying to load it later in the same session I get the following error:


The process cannot access the file because it is being used by another process
[/quote]

If I start a new session then I get the following error when loading the file:



There is an error in XML document (0,0)
[/quote]

The XML document looks like the following:



<LevelSettings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Depth>3</Depth>
<Height>3</Height>
<TileSettings>
<Tile3DSettings xsi:nil="true" />
<Tile3DSettings xsi:nil="true" />
<Tile3DSettings xsi:nil="true" />
<Tile3DSettings xsi:nil="true" />
<Tile3DSettings xsi:nil="true" />
<Tile3DSettings xsi:nil="true" />
<Tile3DSettings xsi:nil="true" />
<Tile3DSettings xsi:nil="true" />
<Tile3DSettings xsi:nil="true" />
<Tile3DSettings xsi:nil="true" />
<Tile3DSettings xsi:nil="true" />
<Tile3DSettings xsi:nil="true" />
<Tile3DSettings xsi:nil="true" />
<Tile3DSettings xsi:nil="true" />
<Tile3DSettings xsi:nil="true" />
<Tile3DSettings xsi:nil="true" />
<Tile3DSettings xsi:nil="true" />
<Tile3DSettings xsi:nil="true" />
<Tile3DSettings xsi:nil="true" />
<Tile3DSettings xsi:nil="true" />
<Tile3DSettings xsi:nil="true" />
<Tile3DSettings xsi:nil="true" />
<Tile3DSettings xsi:nil="true" />
<Tile3DSettings xsi:nil="true" />
<Tile3DSettings xsi:nil="true" />
<Tile3DSettings xsi:nil="true" />
<Tile3DSettings xsi:nil="true" />
</TileSettings>
<VoxelSize>1</VoxelSize>
<Width>3</Width>
</LevelSettings>


Why am I getting these two errors?

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!