I have a class which stores my camera settings

public class SaveData
        public CameraFreeLookSettings CameraSettings;

        /// <summary>
        /// Parameterless constructor for XML Serialization/Deserialization
        /// </summary>
        private SaveData() { }

        public SaveData(CameraFreeLookSettings cameraSettings)
            CameraSettings = cameraSettings;

I'm trying to save those settings using a SaveFileDialog and XmlWriter

SaveData saveData = new SaveData(
                game.ActiveCamera.Write() as CameraFreeLookSettings

            Stream stream = null;

            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.AddExtension = true;
            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)
                    if ((stream = saveFileDialog.OpenFile()) != null)
                        XmlWriterSettings settings = new XmlWriterSettings();
                        settings.ConformanceLevel = ConformanceLevel.Auto;
                        settings.Encoding = System.Text.Encoding.UTF8;
                        settings.Indent = true;
                        //settings.OmitXmlDeclaration = true;

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

This produces what looks like a perfectly normal .XML file

<?xml version="1.0" encoding="utf-8"?>
<SaveData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

When I try loading the .XML file using OpenFileDialog and XmlReader

Stream stream = null;

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

            if (openFileDialog.ShowDialog() == DialogResult.OK)
                    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(SaveData));                // Can this stored elsewhere?

                            bool pants = serializer.CanDeserialize(reader);
                            SaveData saveData = serializer.Deserialize(stream) as SaveData;

                            // Camera
                catch (Exception ex)
                    MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);

I get the following error message

There is an error in XML document (0, 0)

Please can someone explain why this is happening?

You have this line in your writer settings.Encoding = System.Text.Encoding.UTF8; but not your reader. I suspect the reader is looking for UTF16 and not getting what it wants.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

I suspect the reader is looking for UTF16 and not getting what it wants.

I changed the XmlReader creation to

using (XmlReader reader = XmlReader.Create(new StreamReader(stream, System.Text.Encoding.UTF8), settings))

but I still get the same error message.

Solved the problem:

SaveData saveData = serializer.Deserialize(reader) as SaveData;

It should be 'reader' and not 'stream' :D

This article has been dead for over six months. Start a new discussion instead.