我想序列化这个类:
[Serializable]
public class SCMng
{
[XmlElement("SC")]
public ObservableCollection<SC> SCCollection { get; set; }
[XmlIgnore]
public SC SCPreview { get; set; }
public SCMng()
{
this.SCCollection = new ObservableCollection<SC>();
this.SCPreview = new SC();
}
public void SaveMD(string pPath)
{
Directory.CreateDirectory(Path.GetDirectoryName(pPath));
if (System.IO.File.Exists(pPath))
System.IO.File.Delete(pPath);
var ser = new XmlSerializer(typeof(SCMng));
using (var fs = new FileStream(pPath, FileMode.OpenOrCreate))
ser.Serialize(fs, this);
}
}
我的MainWindow.xaml.cs
内写了这个:
SCMng fooBar = new SCMng();
public void Save(){
this.fooBar.SaveMD(AppDomain.CurrentDomain.BaseDirectory + @"\new.xml");
}
我的错误在哪里?我没有例外但是save方法没有创建文件。使用行Directory.CreateDirectory(Path.GetDirectoryName(pPath));
,我得到DirectoryNotFoundException
。也许有人可以帮助我。谢谢!