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)
            {
                try
                {
                    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);
                            //xmlWriter.Close();
                        }
                    }
                }
                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">
  <CameraSettings>
    <FarClip>1100</FarClip>
    <IsIgnoreInput>true</IsIgnoreInput>
    <NearClip>0.1</NearClip>
    <Orientation>
      <X>0.225798175</X>
      <Y>-0.00510073872</Y>
      <Z>-0.00118228782</Z>
      <W>0.9741601</W>
    </Orientation>
    <Position>
      <X>0</X>
      <Y>0</Y>
      <Z>20</Z>
    </Position>
    <Sensitivity>0.3</Sensitivity>
  </CameraSettings>
</SaveData>

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)
            {
                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(SaveData));                // Can this stored elsewhere?

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

                            // Camera
                            game.ActiveCamera.Read(saveData.CameraSettings);
                        }
                    }
                }
                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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.