【发布时间】:2014-12-04 03:01:03
【问题描述】:
我已经做了很多研究,我看到的所有地方都说这应该有效。每次我运行我的程序时,它都会出现以下错误:当程序到达 xdoc.Root.Element 时,“对象引用未设置为对象的实例”.....
这个sn-p在主程序中。
private void btn_save_Click(object sender, EventArgs e)
{
Certification cert = new Certification();
cert.CreateCertification(txt_certlevel.Text, txt_certnum.Text, txt_certagency.Text, dtp_cert.Value);
xmlfunction.Add(cert);
}
而且这个 sn-p 在一个与主程序文件分开的 Class 文件中。
public void Add(Certification certification)
{
XDocument xdoc = XDocument.Load(pathString);
xdoc.Root.Element("Digital_Scuba_Log").Element("Diver").Element("Certifications").Add(new XElement("Certification_Card",
new XElement("Level", certification.Level),
new XElement("Agency", certification.Agency),
new XElement("Number", certification.Number),
new XElement("Date", certification.Date.ToString())
));
xdoc.Save(pathString);
}
任何帮助都会很棒!
【问题讨论】:
-
调试是你的朋友。检查
null值的来源。 -
它似乎在 xdoc 文档类型中。我该如何解决这个问题?
标签: c# xml visual-studio linq-to-xml