【发布时间】:2009-08-05 15:12:52
【问题描述】:
根据我的 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();
}
}
}
部分答案:
我想如果我将 XDocument 更改为 XElement,那么它可以工作。谁能解释一下为什么?
【问题讨论】:
-
您也可以发布您的 XML 文档吗?
标签: c# linq linq-to-xml