I am successfully creating an XML file, however, if the user attempts to create a new XML in the attempt to overwrite the previous version, it instead appends the data; not overwrite.
What is giving me trouble in understanding this is that I first check if the file exists prior to creating the XML file, if it does exist, I delete it and then create the new XML file. I ran my code through the debugger and looked at the directory of where the file is being stored and I can clearly see the file being created, then deleted, and then be recreated.
// Create new KML file
string filepath = STK_TEMP + "/" + stkObjectClass + "-" + stkObjectName + ".kml";
if(File.Exists(filepath))
File.Delete(filepath);
StreamWriter xmlsteam = new StreamWriter(filepath);
using (XmlTextWriter xmlWriter = new XmlTextWriter(xmlsteam))
{
xmlWriter.Formatting = Formatting.Indented;
xmlWriter.Indentation = 2;
// Open KML file for appending data
xmlWriter.WriteStartDocument();
// Write KML
xmlWriter.WriteStartElement("kml", "http://earth.google.com/kml/2.1");
xmlWriter.WriteStartElement("Document");
xmlWriter.WriteStartElement("name");
xmlWriter.WriteString(stkObjectName);
xmlWriter.WriteEndElement();
xmlWriter.WriteStartElement("Placemark");
// Append data from LLA / KML position report
xmlWriter.WriteStartElement("LineString");
xmlWriter.WriteStartElement("altitudeMode");
xmlWriter.WriteString("absolute");
xmlWriter.WriteEndElement();
xmlWriter.WriteStartElement("coordinates");
xmlWriter.WriteString(kmlCoordinateString);
xmlWriter.WriteEndElement(); // END Coordinates
xmlWriter.WriteEndElement(); // END Point
xmlWriter.WriteEndElement(); // END Placemark
bool isFirstPoint = true;
double seconds = 0;
// Create individual poitns for Google Earth playback
foreach (string kmlPoint in kmlCoordinateList)
{
xmlWriter.WriteStartElement("Placemark");
xmlWriter.WriteStartElement("Style");
xmlWriter.WriteStartElement("IconStyle");
xmlWriter.WriteStartElement("Icon");
xmlWriter.WriteStartElement("href");
xmlWriter.WriteString("http://maps.google.com/mapfiles/kml/paddle/wht-blank.png");
xmlWriter.WriteEndElement(); // END href
xmlWriter.WriteEndElement(); // END Icon
xmlWriter.WriteEndElement(); // END IconStyle
xmlWriter.WriteEndElement(); // END Style
xmlWriter.WriteStartElement("TimeStamp");
xmlWriter.WriteStartElement("when");
xmlWriter.WriteString(DateTime.Now.AddSeconds(seconds).ToString("yyyy-MM-ddTHH:mm:ssZ"));
xmlWriter.WriteEndElement(); // END when
xmlWriter.WriteEndElement(); // END TimeStamp
xmlWriter.WriteStartElement("Point");
xmlWriter.WriteStartElement("altitudeMode");
xmlWriter.WriteString("absolute");
xmlWriter.WriteEndElement(); // END altitudeMode
xmlWriter.WriteStartElement("coordinates");
xmlWriter.WriteString(kmlPoint);
xmlWriter.WriteEndElement(); // END coordinates
xmlWriter.WriteEndElement(); // END Point
xmlWriter.WriteEndElement(); // END Placemark
seconds++;
}
xmlWriter.WriteEndElement(); // END KML
// END KML file and close
xmlWriter.WriteEndDocument();
xmlWriter.Flush();
xmlWriter.Close();
}
xmlsteam.Dispose();
If I close out of my application and start it up and trying creating the file, it will overwrite it on the first attempt. Leaving the application open and trying to create a new file with the same file path, it just appends the data.
Any thoughts?
I feel that my XmlTextWriter is not being disposed of properly...