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.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"
備註
部分 軸式方法 會回傳元素或屬性的集合。 此方法僅回傳一個屬性。 有時這被稱為 單元素集(singleton ,與 集合相對)。
Visual Basic 使用者可以使用整合屬性軸來取得具有指定名稱的屬性值。