Extensions.Attributes 方法 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
返回源集合中每个元素的属性的集合。
重载
| Attributes(IEnumerable<XElement>) | 返回源集合中每个元素的属性的集合。 | 
| Attributes(IEnumerable<XElement>, XName) | 返回源集合中经过筛选的每个元素的属性的集合。 集合中仅包括具有匹配 XName 的元素。 | 
注解
Visual Basic用户可以使用集成属性轴从元素集合中检索具有特定名称的属性。
此方法使用延迟执行。
Attributes(IEnumerable<XElement>)
返回源集合中每个元素的属性的集合。
public:
[System::Runtime::CompilerServices::Extension]
 static System::Collections::Generic::IEnumerable<System::Xml::Linq::XAttribute ^> ^ Attributes(System::Collections::Generic::IEnumerable<System::Xml::Linq::XElement ^> ^ source);public static System.Collections.Generic.IEnumerable<System.Xml.Linq.XAttribute> Attributes (this System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> source);public static System.Collections.Generic.IEnumerable<System.Xml.Linq.XAttribute> Attributes (this System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement?> source);static member Attributes : seq<System.Xml.Linq.XElement> -> seq<System.Xml.Linq.XAttribute><Extension()>
Public Function Attributes (source As IEnumerable(Of XElement)) As IEnumerable(Of XAttribute)参数
- source
- IEnumerable<XElement>
一个包含源集合的 IEnumerable<T> 的 XElement。
返回
IEnumerable<T> 的 XAttribute,其中包含源集合中每个元素的属性。
示例
以下示例检索元素集合,然后检索集合中所有元素的所有属性的集合。 请注意,生成的集合仅包含元素Child2的属性Child1,而不是元素的属性Root。
请注意,命名空间属性由此方法返回。
XElement xmlTree = new XElement("Root",  
    new XAttribute(XNamespace.Xmlns + "aw", "http://www.adventure-works.com"),  
    new XAttribute("Att1", "content1"),  
    new XAttribute("Att2", "content2"),  
    new XElement("Child1",  
        new XAttribute("Att1", "content3"),  
        new XAttribute("Att2", "content4")  
    ),  
    new XElement("Child2",  
        new XAttribute("Att1", "content5"),  
        new XAttribute("Att2", "content6")  
    )  
);  
Console.WriteLine(xmlTree);  
Console.WriteLine("-----");  
IEnumerable<XAttribute> attList =  
    from att in xmlTree.DescendantsAndSelf().Attributes()  
    select att;  
foreach (XAttribute att in attList)  
    Console.WriteLine(att);  
Dim xmlTree As XElement = _  
    <Root xmlns:aw="http://www.adventure-works.com" Att1="content1" Att2="content2">  
        <Child1 Att1="content3" Att2="content4"/>  
        <Child2 Att1="content5" Att2="content6"/>  
    </Root>  
Dim attList = _  
    From att In xmlTree.DescendantsAndSelf.Attributes _  
    Select att  
Console.WriteLine(xmlTree)  
Console.WriteLine("-----")  
For Each att As XAttribute In attList  
    Console.WriteLine(att)  
Next  
该示例产生下面的输出:
<Root xmlns:aw="http://www.adventure-works.com" Att1="content1" Att2="content2">  
  <Child1 Att1="content3" Att2="content4" />  
  <Child2 Att1="content5" Att2="content6" />  
</Root>  
-----  
xmlns:aw="http://www.adventure-works.com"  
Att1="content1"  
Att2="content2"  
Att1="content3"  
Att2="content4"  
Att1="content5"  
Att2="content6"  
以下示例相同,但在这种情况下,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"),  
    new XElement(aw + "Child1",  
        new XAttribute(aw + "Att1", "content3"),  
        new XAttribute(aw + "Att2", "content4")  
    ),  
    new XElement(aw + "Child2",  
        new XAttribute(aw + "Att1", "content5"),  
        new XAttribute(aw + "Att2", "content6")  
    )  
);  
Console.WriteLine(xmlTree);  
Console.WriteLine("-----");  
IEnumerable<XAttribute> attList =  
    from att in xmlTree.DescendantsAndSelf().Attributes()  
    select att;  
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 xmlns:aw="http://www.adventure-works.com" aw:Att1="content1" aw:Att2="content2">  
                <aw:Child1 aw:Att1="content3" aw:Att2="content4"/>  
                <aw:Child2 aw:Att1="content5" aw:Att2="content6"/>  
            </aw:Root>  
        Dim attList = _  
            From att In xmlTree.DescendantsAndSelf.Attributes _  
            Select att  
        Console.WriteLine(xmlTree)  
        Console.WriteLine("-----")  
        For Each att As XAttribute In attList  
            Console.WriteLine(att)  
        Next  
    End Sub  
End Module  
该示例产生下面的输出:
<aw:Root xmlns:aw="http://www.adventure-works.com" aw:Att1="content1" aw:Att2="content2">  
  <aw:Child1 aw:Att1="content3" aw:Att2="content4" />  
  <aw:Child2 aw:Att1="content5" aw:Att2="content6" />  
</aw:Root>  
-----  
xmlns:aw="http://www.adventure-works.com"  
aw:Att1="content1"  
aw:Att2="content2"  
aw:Att1="content3"  
aw:Att2="content4"  
aw:Att1="content5"  
aw:Att2="content6"  
注解
请注意,与其他一些 XML 编程接口不同,在LINQ to XML中,命名空间显示为属性。
尽管Visual Basic用户可以使用集成属性轴从元素集合中检索具有指定名称的属性,但没有集成Visual Basic轴检索集合中所有元素的所有属性。
此方法使用延迟执行。
另请参阅
适用于
Attributes(IEnumerable<XElement>, XName)
返回源集合中经过筛选的每个元素的属性的集合。 集合中仅包括具有匹配 XName 的元素。
public:
[System::Runtime::CompilerServices::Extension]
 static System::Collections::Generic::IEnumerable<System::Xml::Linq::XAttribute ^> ^ Attributes(System::Collections::Generic::IEnumerable<System::Xml::Linq::XElement ^> ^ source, System::Xml::Linq::XName ^ name);public static System.Collections.Generic.IEnumerable<System.Xml.Linq.XAttribute> Attributes (this System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> source, System.Xml.Linq.XName name);public static System.Collections.Generic.IEnumerable<System.Xml.Linq.XAttribute> Attributes (this System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement?> source, System.Xml.Linq.XName? name);static member Attributes : seq<System.Xml.Linq.XElement> * System.Xml.Linq.XName -> seq<System.Xml.Linq.XAttribute><Extension()>
Public Function Attributes (source As IEnumerable(Of XElement), name As XName) As IEnumerable(Of XAttribute)参数
- source
- IEnumerable<XElement>
一个包含源集合的 IEnumerable<T> 的 XElement。
返回
IEnumerable<T> 的 XAttribute,其中包含源集合中经过筛选的每个元素的属性集合。 集合中仅包括具有匹配 XName 的元素。
示例
以下示例检索元素集合,在本例中包括 Child1 元素和 Child2 元素。 然后,它检索具有名称 Att1的子集合的所有属性。
XElement xmlTree = new XElement("Root",  
    new XAttribute("Att1", "content1"),  
    new XAttribute("Att2", "content2"),  
    new XElement("Child1",  
        new XAttribute("Att1", "content3"),  
        new XAttribute("Att2", "content4")  
    ),  
    new XElement("Child2",  
        new XAttribute("Att1", "content5"),  
        new XAttribute("Att2", "content6")  
    )  
);  
IEnumerable<XAttribute> attList = from att in xmlTree.Elements().Attributes("Att1")  
                                  select att;  
foreach (XAttribute att in attList)  
    Console.WriteLine(att);  
Dim xmlTree As XElement = _  
    <Root Att1="content1" Att2="content2">  
        <Child1 Att1="content3" Att2="content4">  
        </Child1>  
        <Child2 Att1="content5" Att2="content6">  
        </Child2>  
    </Root>  
Dim attList = From att In xmlTree.Elements.Attributes("Att1") _  
                          Select att  
For Each att As XAttribute In attList  
    Console.WriteLine(att)  
Next  
该示例产生下面的输出:
Att1="content3"  
Att1="content5"  
注解
请注意,与其他一些 XML 编程接口不同,在LINQ to XML中,命名空间显示为属性。
此方法使用延迟执行。