XElement.Attributes メソッド

定義

この要素の属性のコレクションを返します。

オーバーロード

Attributes()

この要素の属性のコレクションを返します。

Attributes(XName)

この要素の属性のフィルター処理されたコレクションを返します。 一致する XName を持つ属性のみがコレクションに含められます。

注釈

このメソッドは遅延実行を使用します。

Attributes()

Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs

この要素の属性のコレクションを返します。

public:
 System::Collections::Generic::IEnumerable<System::Xml::Linq::XAttribute ^> ^ Attributes();
public System.Collections.Generic.IEnumerable<System.Xml.Linq.XAttribute> Attributes ();
member this.Attributes : unit -> seq<System.Xml.Linq.XAttribute>
Public Function Attributes () As IEnumerable(Of XAttribute)

戻り値

この要素の XAttribute 属性の IEnumerable<T>

次の例では、2 つの属性を持つ 要素を作成します。 次に、これを使用して 要素のすべての属性を取得します。

XElement xmlTree = new XElement("Root",
    new XAttribute("Att1", "content1"),
    new XAttribute("Att2", "content2")
);
IEnumerable<XAttribute> attList =
    from at in xmlTree.Attributes()
    select at;
foreach (XAttribute att in attList)
    Console.WriteLine(att);
Dim xmlTree As XElement = <Root Att1="content1" Att2="content2"/>

Dim attList As IEnumerable(Of XAttribute) = _
From at In xmlTree.Attributes() _
Select at

For Each att In attList
    Console.WriteLine(att)
Next

この例を実行すると、次の出力が生成されます。

Att1="content1"
Att2="content2"

同じ例を次に示しますが、この場合、XML は名前空間にあります。 詳細については、「 XML 名前空間の操作」を参照してください。

XNamespace aw = "http://www.adventure-works.com";
XElement xmlTree = new XElement(aw + "Root",
    new XAttribute(aw + "Att1", "content1"),
    new XAttribute(aw + "Att2", "content2"),
    new XAttribute(XNamespace.Xmlns + "aw", "http://www.adventure-works.com")
);
IEnumerable<XAttribute> attList =
    from at in xmlTree.Attributes()
    select at;
foreach (XAttribute att in attList)
    Console.WriteLine(att);
Imports <xmlns:aw="http://www.adventure-works.com">

Module Module1
    Sub Main()
        Dim xmlTree As XElement = <aw:Root aw:Att1="content1" aw:Att2="content2"/>

        Dim attList As IEnumerable(Of XAttribute) = _
            From at In xmlTree.Attributes() _
            Select at

        For Each att In attList
            Console.WriteLine(att)
        Next
    End Sub
End Module

この例を実行すると、次の出力が生成されます。

aw:Att1="content1"
aw:Att2="content2"
xmlns:aw="http://www.adventure-works.com"

注釈

返されるコレクション内の属性は、 要素に追加された順序です。 XML ツリーが XML から解析された場合、属性はドキュメントの順序で返されます。

このメソッドは遅延実行を使用します。

こちらもご覧ください

適用対象

Attributes(XName)

Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs

この要素の属性のフィルター処理されたコレクションを返します。 一致する XName を持つ属性のみがコレクションに含められます。

public:
 System::Collections::Generic::IEnumerable<System::Xml::Linq::XAttribute ^> ^ Attributes(System::Xml::Linq::XName ^ name);
public System.Collections.Generic.IEnumerable<System.Xml.Linq.XAttribute> Attributes (System.Xml.Linq.XName name);
public System.Collections.Generic.IEnumerable<System.Xml.Linq.XAttribute> Attributes (System.Xml.Linq.XName? name);
member this.Attributes : System.Xml.Linq.XName -> seq<System.Xml.Linq.XAttribute>
Public Function Attributes (name As XName) As IEnumerable(Of XAttribute)

パラメーター

name
XName

照合する XName

戻り値

この要素の属性を格納している XAttributeIEnumerable<T>。 一致する XName を持つ属性のみがコレクションに含められます。

次の例では、この を使用します。

XElement xmlTree = new XElement("Root",
    new XAttribute("Att1", "content1"),
    new XAttribute("Att2", "content2")
);
IEnumerable<XAttribute> attList = xmlTree.Attributes("Att1");
foreach (XAttribute att in attList)
    Console.WriteLine(att);
Dim xmlTree As XElement = <Root Att1="content1" Att2="content2"/>

Dim attList As IEnumerable(Of XAttribute) = xmlTree.Attributes("Att1")

For Each att In attList
    Console.WriteLine(att)
Next

この例を実行すると、次の出力が生成されます。

Att1="content1"

同じ例を次に示しますが、この場合、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 + "Att1", "content1"),
    new XAttribute(aw + "Att2", "content2")
);
IEnumerable<XAttribute> attList = xmlTree.Attributes(aw + "Att1");
foreach (XAttribute att in attList)
    Console.WriteLine(att);
Imports <xmlns:aw="http://www.adventure-works.com">

Module Module1
    Sub Main()
        Dim xmlTree As XElement = <aw:Root aw:Att1="content1" aw:Att2="content2"/>

        Dim attList As IEnumerable(Of XAttribute) = xmlTree.Attributes(GetXmlNamespace(aw) + "Att1")

        For Each att In attList
            Console.WriteLine(att)
        Next
    End Sub
End Module

この例を実行すると、次の出力が生成されます。

aw:Att1="content1"

注釈

属性名は、要素内で一意である必要があります。 したがって、1 つの属性のみを含むコレクションを返すか、空のコレクションを返すことができます。

このメソッドは遅延実行を使用します。

こちらもご覧ください

適用対象