XElement.Attribute(XName) メソッド

定義

指定したXAttributeを持つこのXElementXNameを返します。

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を持つ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"

注釈

一部 の軸メソッドは、 要素または属性のコレクションを返します。 このメソッドは、1 つの属性のみを返します。 これは、(コレクションとは対照的に) シングルトンと呼ばれることもあります。

Visual Basicユーザーは、統合属性軸を使用して、指定した名前の属性の値を取得できます。

適用対象

こちらもご覧ください