XElement.Attribute(XName) 方法

定義

傳回這個 XAttributeXElement,其具有指定的 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

要取得之 XNameXAttribute

傳回

XAttribute

具有指定之 XAttributeXName,如果沒有具有指定名稱的屬性,則為 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使用者可以使用整合屬性軸來擷取具有指定名稱的屬性值。

適用於

另請參閱