XElement.Attributes 方法

定义

返回此元素的属性集合。Returns a collection of attributes of this element.

重载

Attributes()

返回此元素的属性集合。Returns a collection of attributes of this element.

Attributes(XName)

返回经过筛选的此元素的属性集合。Returns a filtered collection of attributes of this element. 集合中仅包括具有匹配 XName 的元素。Only elements that have a matching XName are included in the collection.

注解

此方法使用延迟执行。This method uses deferred execution.

Attributes()

返回此元素的属性集合。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)

返回

此元素的属性的 IEnumerable<T>XAttributeAn IEnumerable<T> of XAttribute of attributes of this element.

示例

下面的示例创建一个具有两个属性的元素。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"  

下面是同一个示例,但在这种情况下,XML 位于命名空间中。The following is the same example, but in this case the XML is in a namespace. 有关详细信息,请参阅使用XML 命名空间For more information, see Working 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"  

注解

返回的集合中的属性按照添加到元素中的顺序排列。The attributes in the returned collection are in the order that they were added to the element. 如果 XML 树是从 XML 进行分析的,则这些属性将按文档顺序返回。If the XML tree was parsed from XML, the attributes are returned in document order.

此方法使用延迟执行。This method uses deferred execution.

另请参阅

Attributes(XName)

返回经过筛选的此元素的属性集合。Returns a filtered collection of attributes of this element. 集合中仅包括具有匹配 XName 的元素。Only elements 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);
member this.Attributes : System.Xml.Linq.XName -> seq<System.Xml.Linq.XAttribute>
Public Function Attributes (name As XName) As IEnumerable(Of XAttribute)

参数

name
XName

要匹配的 XNameThe XName to match.

返回

IEnumerable<T>XAttribute,其中包含此元素的属性。An IEnumerable<T> of XAttribute that contains the attributes of this element. 集合中仅包括具有匹配 XName 的元素。Only elements that have a matching XName are included in the collection.

示例

下面的示例使用此。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"  

下面是同一个示例,但在这种情况下,XML 位于命名空间中。The following is the same example, but in this case the XML is in a namespace. 有关详细信息,请参阅使用XML 命名空间For more information, see Working 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"  

注解

属性名称在元素中必须是唯一的。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.

另请参阅

适用于