Edytuj

Udostępnij za pośrednictwem


XElement.Attributes Method

Definition

Returns a collection of attributes of this element.

Overloads

Attributes()

Returns a collection of attributes of this element.

Attributes(XName)

Returns a filtered collection of attributes of this element. Only attributes that have a matching XName are included in the collection.

Remarks

This method uses deferred execution.

Attributes()

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

Returns a collection of attributes of this element.

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)

Returns

An IEnumerable<T> of XAttribute of attributes of this element.

Examples

The following example creates an element with two attributes. It then uses this to retrieve all attributes of the element.

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

This example produces the following output:

Att1="content1"
Att2="content2"

The following is the same example, but in this case the XML is in a namespace. For more information, see Work with XML Namespaces.

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

This example produces the following output:

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

Remarks

The attributes in the returned collection are in the order that they were added to the element. If the XML tree was parsed from XML, the attributes are returned in document order.

This method uses deferred execution.

See also

Applies to

Attributes(XName)

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

Returns a filtered collection of attributes of this element. Only attributes that have a matching XName are included in the collection.

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)

Parameters

name
XName

The XName to match.

Returns

An IEnumerable<T> of XAttribute that contains the attributes of this element. Only attributes that have a matching XName are included in the collection.

Examples

The following example uses this .

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

This example produces the following output:

Att1="content1"

The following is the same example, but in this case the XML is in a namespace. For more information, see Work with XML Namespaces.

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

This example produces the following output:

aw:Att1="content1"

Remarks

Attribute names must be unique within an element. Therefore, this can return either a collection that contains only one attribute, or it can return an empty collection.

This method uses deferred execution.

See also

Applies to