XElement.Attributes Méthode

Définition

Retourne une collection d’attributs de cet élément.

Surcharges

Nom Description
Attributes()

Retourne une collection d’attributs de cet élément.

Attributes(XName)

Retourne une collection filtrée d’attributs de cet élément. Seuls les attributs qui ont une correspondance XName sont inclus dans la collection.

Remarques

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

Attributes()

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

Retourne une collection d’attributs de cet élément.

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)

Retours

Attributs IEnumerable<T>XAttribute de cet élément.

Exemples

L’exemple suivant crée un élément avec deux attributs. Il l’utilise ensuite pour récupérer tous les attributs de l’élément.

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

Cet exemple produit la sortie suivante :

Att1="content1"
Att2="content2"

Voici le même exemple, mais dans ce cas, le code XML se trouve dans un espace de noms. Pour plus d’informations, consultez Utiliser des espaces de noms XML.

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

Cet exemple produit la sortie suivante :

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

Remarques

Les attributs de la collection retournée sont dans l’ordre dans lequel ils ont été ajoutés à l’élément. Si l’arborescence XML a été analysée à partir de XML, les attributs sont retournés dans l’ordre du document.

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

Voir aussi

S’applique à

Attributes(XName)

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

Retourne une collection filtrée d’attributs de cet élément. Seuls les attributs qui ont une correspondance XName sont inclus dans la 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)

Paramètres

name
XName

À XName mettre en correspondance.

Retours

Qui IEnumerable<T>XAttribute contient les attributs de cet élément. Seuls les attributs qui ont une correspondance XName sont inclus dans la collection.

Exemples

L’exemple suivant utilise ceci .

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

Cet exemple produit la sortie suivante :

Att1="content1"

Voici le même exemple, mais dans ce cas, le code XML se trouve dans un espace de noms. Pour plus d’informations, consultez Utiliser des espaces de noms 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")
);
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

Cet exemple produit la sortie suivante :

aw:Att1="content1"

Remarques

Les noms d’attributs doivent être uniques dans un élément. Par conséquent, cela peut retourner une collection qui contient un seul attribut, ou elle peut retourner une collection vide.

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

Voir aussi

S’applique à