我做的一个Winforms小闹钟程序时,出现一个非常莫名其妙的故障:
我是将闹钟信息保存于XML文件中,当我保存闹响的音乐文件的路径时候,发现无法保存,并且不报错!且之后也不能再保存闹钟信息!!但是如果不设置闹响音乐文件路径的话,则没有问题!
代码:
public
class
AlertsManager:List
<
Alert
>
{
private string xmlFileName = " /Alerts.xml " ;
private XmlDocument docFile;
private List < string > exceptions = new List < string > ();
......
public new void Add(Alert alert)
{
if (docFile.SelectNodes( " //Alert[@Time=' " + alert.Time.ToLongTimeString() + " '] " ).Count <= 0 )
{
XmlElement newAlert = docFile.CreateNode(XmlNodeType.Element, " Alert " , null ) as XmlElement;
XmlElement newAlertMusicFile = docFile.CreateElement( " MusicFilePath " );
...
newAlertMusicFile.InnerXml = " <![CDATA[ " + alert.MusicFile + " ]]> " ;
...
newAlert.AppendChild(newAlertMusicFile);
...
docFile.DocumentElement.AppendChild(newAlert);
docFile.Save(xmlFileName);
base .Add(alert);
......
{
private string xmlFileName = " /Alerts.xml " ;
private XmlDocument docFile;
private List < string > exceptions = new List < string > ();
......
public new void Add(Alert alert)
{
if (docFile.SelectNodes( " //Alert[@Time=' " + alert.Time.ToLongTimeString() + " '] " ).Count <= 0 )
{
XmlElement newAlert = docFile.CreateNode(XmlNodeType.Element, " Alert " , null ) as XmlElement;
XmlElement newAlertMusicFile = docFile.CreateElement( " MusicFilePath " );
...
newAlertMusicFile.InnerXml = " <![CDATA[ " + alert.MusicFile + " ]]> " ;
...
newAlert.AppendChild(newAlertMusicFile);
...
docFile.DocumentElement.AppendChild(newAlert);
docFile.Save(xmlFileName);
base .Add(alert);
......
后来发现CSDN上还有一个人和我遇到了同样的情况(他保存图片路径),后来他也解决了.
我按照其解决办法,将
private string xmlFileName="//Alerts.xml";
改成了
private string xmlFileName = Application.StartupPath+@"/Alerts.xml";
问题解决!!!
希望大家记住,避免发生这种怪错误.