I've made a simple tool for generating an XML file. I have been using this tool over the last couple of days and no issues have arisen from it. Then yesterday I went to use it, and I'm getting the following error:
XmlException: Document element did not appear. file:///C:/DarkRideSettings/DarkrideSettings.xml Line 1, position 1.
From my understanding, this error is saying that the computer cannot find the XML file in the stated location within the code. The thing is, I literally used this code the day before and the XML file is where it should be.
My code for writing my XML is as follows:
public void WriteXMLFile()
{
// location of the file
string _filePath = "C:\\DarkRideSettings\\DarkrideSettings.xml";
XmlDocument _xmlDoc = new XmlDocument();
// if the file exists
if (File.Exists(_filePath))
{
// load it in
_xmlDoc.Load(_filePath);
// clear out the previous data
_xmlDoc.RemoveAll();
// create the main root node
XmlNode rootNode = _xmlDoc.CreateElement("Settings");
_xmlDoc.AppendChild(rootNode);
// corners node (next layer down)
XmlElement _cornerNode = _xmlDoc.CreateElement("Screen_Corners");
_xmlDoc.DocumentElement.PrependChild(_cornerNode);
#region Top Left Corners XYZ Values
// indent top left corner value to screen corners
XmlElement _topLeftNode = _xmlDoc.CreateElement("Top_Left");
_cornerNode.AppendChild(_topLeftNode);
// set the XYZ of the top left values
XmlElement _topLeftXNode = _xmlDoc.CreateElement("TopLeftX");
// take string value and convert to float for use in final calculation
float _topLeftXFloat = Convert.ToSingle(_screenWidthString);
float _topLeftX = -_topLeftXFloat / 2.0f;
_topLeftXNode.InnerText = Convert.ToString(_topLeftX);
XmlElement _topLeftYNode = _xmlDoc.CreateElement("TopLeftY");
_topLeftYNode.InnerText = _screenHeightString;
XmlElement _topLeftZNode = _xmlDoc.CreateElement("TopLeftZ");
float _topLeftZFloat = Convert.ToSingle(_distanceFromScreenString);
float _topLeftZ = _topLeftZFloat / 2.0f;
_topLeftZNode.InnerText = Convert.ToString(_topLeftZ);
// indent these values to the top_left value in XML
_topLeftNode.AppendChild(_topLeftXNode);
_topLeftNode.AppendChild(_topLeftYNode);
_topLeftNode.AppendChild(_topLeftZNode);
#endregion
_xmlDoc.Save(_filePath);
}
}
This code would happily generate my XML file (please note, I've omitted some bits of code from this method to save on space, nothing of any major importance).
Can anyone see if I'm missing something from my code that I should have (I honestly have no idea, as I said, it worked fine yesterday!) or how I can defeat this error. My XML file is to be found at its required location and, even if it wasn't, my code should still write a new one!
Please help me.