XAttribute 类概述

属性是与元素关联的名称/值对。 XAttribute 类表示 XML 属性。

使用 LINQ to XML 中的属性,与使用元素非常相似。 它们的构造函数相似。 用于检索它们的集合的方法相似。 属性集合的 LINQ 查询表达式与元素集合的 LINQ 查询表达式看起来非常相似。

将属性添加到元素中的顺序会保留下来。 也就是说,当循环访问属性时,所见到的属性顺序与它们的添加顺序相同。

XAttribute 构造函数

下面的 XAttribute 类构造函数是你将最常使用的构造函数之一:

构造函数 说明
XAttribute(XName name, object content) 创建一个 XAttribute 对象。 name 参数指定属性的名称;content 指定属性的内容。

示例:创建具有属性的元素

下面的示例演示创建包含属性的元素的常见任务。

XElement phone = new XElement("Phone",
    new XAttribute("Type", "Home"),
    "555-555-5555");
Console.WriteLine(phone);
Dim phone As XElement = <Phone Type="Home">555-555-5555</Phone>
Console.WriteLine(phone)

该示例产生下面的输出:

<Phone Type="Home">555-555-5555</Phone>

示例:属性的函数构造

可以构造与 XAttribute 对象的构造一致的 XElement 对象,如以下示例中所示:

XElement c = new XElement("Customers",
    new XElement("Customer",
        new XElement("Name", "John Doe"),
        new XElement("PhoneNumbers",
            new XElement("Phone",
                new XAttribute("type", "home"),
                "555-555-5555"),
            new XElement("Phone",
                new XAttribute("type", "work"),
                "666-666-6666")
        )
    )
);
Console.WriteLine(c);
Dim c As XElement = _
    <Customers>
        <Customer>
            <Name>John Doe</Name>
            <PhoneNumbers>
                <Phone type="home">555-555-5555</Phone>
                <Phone type="work">666-666-6666</Phone>
            </PhoneNumbers>
        </Customer>
    </Customers>
Console.WriteLine(c)

该示例产生下面的输出:

<Customers>
  <Customer>
    <Name>John Doe</Name>
    <PhoneNumbers>
      <Phone type="home">555-555-5555</Phone>
      <Phone type="work">666-666-6666</Phone>
    </PhoneNumbers>
  </Customer>
</Customers>

属性不是节点

属性与元素之间有些区别。 XAttribute 对象不是 XML 树中的节点。 它们是与 XML 元素关联的名称/值对。 与文档对象模型 (DOM) 相比,这更加贴切地反映了 XML 结构。 虽然 XAttribute 对象实际上不是 XML 树的节点,但使用 XAttribute 对象与使用 XElement 对象非常相似。

这一区别仅对编写在节点级使用 XML 树的代码的开发人员特别重要。 许多开发人员不会关心这种区别。

另请参阅