XElement.Attribute(XName) 方法

定义

返回XAttribute具有指定XElement值的此XName项。

public:
 System::Xml::Linq::XAttribute ^ Attribute(System::Xml::Linq::XName ^ name);
public System.Xml.Linq.XAttribute Attribute(System.Xml.Linq.XName name);
public System.Xml.Linq.XAttribute? Attribute(System.Xml.Linq.XName name);
member this.Attribute : System.Xml.Linq.XName -> System.Xml.Linq.XAttribute
Public Function Attribute (name As XName) As XAttribute

参数

name
XName

XName 获取的 XAttribute

返回

XAttribute具有指定XName名称null的属性;如果没有具有指定名称的属性。

示例

以下示例创建一个具有属性的元素。 然后,它使用此方法检索属性。

XElement xmlTree = new XElement("Root",
    new XAttribute("Att", "attribute content")
);
XAttribute att = xmlTree.Attribute("Att");
Console.WriteLine(att);
Dim xmlTree As XElement = <Root Att="attribute content"/>

Dim att As XAttribute = xmlTree.Attribute("Att")
Console.WriteLine(att)

此示例生成以下输出:

Att="attribute content"

下面是相同的示例,但在这种情况下,XML 位于命名空间中。 有关详细信息,请参阅 “使用 XML 命名空间”。

XNamespace aw = "http://www.adventure-works.com";
XElement xmlTree = new XElement(aw + "Root",
    new XAttribute(XNamespace.Xmlns + "aw", "http://www.adventure-works.com"),
    new XAttribute(aw + "Att", "attribute content")
);
XAttribute att = xmlTree.Attribute(aw + "Att");
Console.WriteLine(att);
Imports <xmlns:aw="http://www.adventure-works.com">

Module Module1
    Sub Main()
        Dim xmlTree As XElement = <aw:Root aw:Att="attribute content"/>

        Dim att As XAttribute = xmlTree.Attribute(GetXmlNamespace(aw) + "Att")
        Console.WriteLine(att)
    End Sub
End Module

此示例生成以下输出:

aw:Att="attribute content"

注解

某些 轴方法 返回元素或属性的集合。 此方法仅返回单个属性。 有时这称为 单一实例 (与 集合相反)。

Visual Basic用户可以使用集成属性轴来检索具有指定名称的属性的值。

适用于

另请参阅