Extensions.Attributes Méthode

Définition

Retourne une collection des attributs de chaque élément de la collection source.

Surcharges

Attributes(IEnumerable<XElement>)

Retourne une collection des attributs de chaque élément de la collection source.

Attributes(IEnumerable<XElement>, XName)

Retourne une collection filtrée des attributs de chaque élément de la collection source. Seuls les éléments avec un XName correspondant sont inclus dans la collection.

Remarques

Les utilisateurs de Visual Basic peuvent utiliser l’axe d’attributs intégré pour récupérer des attributs portant un nom particulier à partir d’une collection d’éléments.

Cette méthode utilise l'exécution différée.

Attributes(IEnumerable<XElement>)

Retourne une collection des attributs de chaque élément de la collection source.

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)

Paramètres

source
IEnumerable<XElement>

IEnumerable<T> de XElement qui contient la collection source.

Retours

IEnumerable<T> de XAttribute qui contient les attributs de chaque élément de la collection source.

Exemples

L’exemple suivant récupère une collection d’éléments, puis récupère une collection de tous les attributs de tous les éléments de la collection. Notez que la collection résultante inclut uniquement les attributs des Child1 éléments et Child2 , et non les attributs de l’élément Root .

Notez que l’attribut d’espace de noms est retourné par cette méthode.

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  

Cet exemple produit la sortie suivante :

<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"  

L’exemple suivant est le même, mais dans ce cas, le xml se trouve dans un espace de noms. Pour plus d’informations, consultez Utiliser des espaces de noms XML. Notez que l’attribut d’espace de noms est inclus dans la collection retournée.

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  

Cet exemple produit la sortie suivante :

<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"  

Remarques

Notez que, contrairement à d’autres interfaces de programmation XML, dans LINQ to XML, les espaces de noms sont exposés en tant qu’attributs.

Bien que les utilisateurs Visual Basic puissent utiliser l’axe d’attributs intégré pour récupérer des attributs portant un nom spécifié à partir d’une collection d’éléments, il n’existe aucun axe Visual Basic intégré pour récupérer tous les attributs de tous les éléments d’une collection.

Cette méthode utilise l'exécution différée.

Voir aussi

S’applique à

Attributes(IEnumerable<XElement>, XName)

Retourne une collection filtrée des attributs de chaque élément de la collection source. Seuls les éléments avec un XName correspondant sont inclus dans la collection.

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)

Paramètres

source
IEnumerable<XElement>

IEnumerable<T> de XElement qui contient la collection source.

name
XName

XName à mettre en correspondance.

Retours

IEnumerable<T> de XAttribute qui contient une collection filtrée des attributs de chaque élément de la collection source. Seuls les éléments avec un XName correspondant sont inclus dans la collection.

Exemples

L’exemple suivant récupère une collection d’éléments, qui dans ce cas inclut les Child1 éléments et Child2 . Il récupère ensuite tous les attributs de cette collection enfant avec le nom 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  

Cet exemple produit la sortie suivante :

Att1="content3"  
Att1="content5"  

Remarques

Notez que, contrairement à d’autres interfaces de programmation XML, dans LINQ to XML, les espaces de noms sont exposés en tant qu’attributs.

Cette méthode utilise l'exécution différée.

Voir aussi

S’applique à