XElement.Attributes 메서드

정의

이 요소의 특성 컬렉션을 반환합니다.

오버로드

Attributes()

이 요소의 특성 컬렉션을 반환합니다.

Attributes(XName)

이 요소의 필터링된 특성 컬렉션을 반환합니다. 일치하는 XName이 있는 특성만 컬렉션에 포함됩니다.

설명

이 메서드는 지연된 실행을 사용합니다.

Attributes()

이 요소의 특성 컬렉션을 반환합니다.

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)

반환

IEnumerable<XAttribute>

이 요소에 있는 특성에 대한 IEnumerable<T>XAttribute입니다.

예제

다음 예제에서는 두 개의 특성을 가진 요소를 만듭니다. 그런 다음, 이를 사용하여 요소의 모든 특성을 검색합니다.

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)

이 요소의 필터링된 특성 컬렉션을 반환합니다. 일치하는 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입니다.

반환

IEnumerable<XAttribute>

이 요소의 특성이 들어 있는 IEnumerable<T>XAttribute입니다. 일치하는 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"  

설명

특성 이름은 요소 내에서 고유해야 합니다. 따라서 하나의 특성만 포함된 컬렉션을 반환하거나 빈 컬렉션을 반환할 수 있습니다.

이 메서드는 지연된 실행을 사용합니다.

추가 정보

적용 대상