Читати англійською Редагувати

Поділитися через


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.

C#
public System.Collections.Generic.IEnumerable<System.Xml.Linq.XAttribute> Attributes();

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.

C#
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);

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.

C#
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);

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

.NET 10 та інші версії
Продукт Версії
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

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.

C#
public System.Collections.Generic.IEnumerable<System.Xml.Linq.XAttribute> Attributes(System.Xml.Linq.XName name);
C#
public System.Collections.Generic.IEnumerable<System.Xml.Linq.XAttribute> Attributes(System.Xml.Linq.XName? name);

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 .

C#
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);

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.

C#
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);

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

.NET 10 та інші версії
Продукт Версії
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0