根据我的Linq书,这个稍微修改过的示例应该可以工作。
为什么它告诉我”
对象引用未设置为对象的实例
“?
using System;
using System.Xml.Linq;
namespace TestNoAttribute
{
class Program
{
static void Main(string[] args)
{
XDocument xdoc = new XDocument(
new XElement("employee",
new XAttribute("id", "23"),
new XElement("firstName", new XAttribute("display", "true"), "Jim"),
new XElement("lastName", new XAttribute("display", "false"), "Smith")));
XElement element = xdoc.Element("firstName");
XAttribute attribute = element.Attribute("display"); //error
Console.WriteLine(xdoc);
Console.ReadLine();
}
}
}
部分回答:
我知道如果我换衣服
X文档
到
X元素
,然后它就工作了。有人能解释一下吗
为什么?
?