【发布时间】:2014-07-21 22:16:58
【问题描述】:
这是我当前的 XML 结构
<root>
<sublist>
<sub a="test" b="test" c="test"></sub>
</sublist>
</root>
我使用以下 C#,但尝试执行时出错
public static void writeSub(string a,string b,string c)
{
XDocument xDoc = XDocument.Load(sourceFile);
XElement root = new XElement("sub");
root.Add(new XAttribute("a", a), new XAttribute("b", b),
new XAttribute("c", c));
xDoc.Element("sub").Add(root);
xDoc.Save(sourceFile);
}
我哪里弄错了?
错误是
nullreferenceexception was unhandled
【问题讨论】:
-
使用调试器找出什么是空的。
-
你认为
xDoc.Element("sub")是什么?
标签: c# xml linq-to-xml xelement xattribute