XElement.Attribute(XName) 方法 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
返回具有指定 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.XAttributePublic 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用户可以使用集成属性轴来检索具有指定名称的属性的值。