日期:2014-05-17  浏览次数:21037 次

xml不能保存 和 文本不能写入
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            createXml();

            StreamWriter sw = new StreamWriter("only.txt");
            sw.Write("0");
            sw.Close();
        }

private void createXml()
        {
            XmlDocument xmlDoc = new XmlDocument();
            
            XmlElement xe = xmlDoc.CreateElement("titles");
            string name;
            for(int index=0;index <this.listView1.Items.Count;index++)
            {
                name = this.listView1.Items[index].Text;
                XmlElement xe1 = xmlDoc.CreateElement("title");
                xe1.SetAttribute("name",name);
                xe1.SetAttribute("path", pathList[name]);

                xe.AppendChild(xe1);
            }
            xmlDoc.AppendChild(xe);
            xmlDoc.Save("title.xml");
        }

以前代码是没问题的,今天就突然出现了xml没保存,文本不能写入数据。  把closing事件的内容放到load事件或者放到Program.cs里就能用。为什么?

------解决方案--------------------
不一定会保存到Debug目录下。
你要是在保存前(即你上面代码的窗体关闭前),有OpenFileDialog或SaveFileDialog等操作,会改变系统初始路径,
你保存xml文件时如果不是全路径,会保存到系统初始路径里。
------解决方案--------------------
一般都是保存到和exe文件相同目录下  Application.StartPath+@"\xxx.txt"