0

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...

enter image description here

4

4 回答 4

3

利用

StreamWriter xmlsteam = new StreamWriter(filepath,false);
于 2013-07-12T18:45:28.837 回答
3

将分配更改为xmlsteam以下内容:

StreamWriter xmlsteam = new StreamWriter(filepath, false);

false作为参数添加会强制它覆盖文件而不是附加:

http://msdn.microsoft.com/en-us/library/36b035cb.aspx

作为一项附加措施,请在创建后立即添加以下行xmlsteam

xmlsteam.AutoFlush = true;

这将确保文件的所有缓冲区在每次文件操作后自动刷新。

在进一步研究之后,我认为问题在于您xmlsteam.Dispose()在处理依赖它的资源之前调用。您应该将代码的末尾修改为以下内容:

        xmlWriter.WriteEndElement(); // END KML

        // END KML file and close
        xmlWriter.WriteEndDocument();
        xmlWriter.Flush();
        xmlWriter.Close();

    }  // end of 'using' block

xmlsteam.Dispose();
于 2013-07-12T18:46:58.933 回答
0

请参阅StreamWriter 构造函数文档。第二个参数确定是否覆盖或附加现有文件。从文档中,通过

true 将数据附加到文件中;false 覆盖文件。如果指定的文件不存在,则该参数无效,构造函数创建一个新文件。

于 2013-07-12T18:49:30.377 回答
0

我意识到我的问题......不幸的是,这是我一个非常粗心的错误。创建/覆盖现有文件时,不会重置存储在我的变量中的数据。

感谢大家试图帮助我解决这个问题。

于 2013-07-12T19:40:22.023 回答