Extensions.Elements Méthode

Définition

Retourne une collection des éléments enfants de chaque élément et document de la collection source.

Surcharges

Elements<T>(IEnumerable<T>)

Retourne une collection des éléments enfants de chaque élément et document de la collection source.

Elements<T>(IEnumerable<T>, XName)

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

Remarques

Visual Basic contient un axe d’éléments intégré qui vous permet de rechercher tous les éléments enfants avec un spécifié XName pour chaque élément de la collection source.

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

Elements<T>(IEnumerable<T>)

Retourne une collection des éléments enfants de chaque élément et document de la collection source.

public:
generic <typename T>
 where T : System::Xml::Linq::XContainer[System::Runtime::CompilerServices::Extension]
 static System::Collections::Generic::IEnumerable<System::Xml::Linq::XElement ^> ^ Elements(System::Collections::Generic::IEnumerable<T> ^ source);
public static System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> Elements<T> (this System.Collections.Generic.IEnumerable<T> source) where T : System.Xml.Linq.XContainer;
public static System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> Elements<T> (this System.Collections.Generic.IEnumerable<T?> source) where T : System.Xml.Linq.XContainer;
static member Elements : seq<'T (requires 'T :> System.Xml.Linq.XContainer)> -> seq<System.Xml.Linq.XElement> (requires 'T :> System.Xml.Linq.XContainer)
<Extension()>
Public Function Elements(Of T As XContainer) (source As IEnumerable(Of T)) As IEnumerable(Of XElement)

Paramètres de type

T

Type des objets de source, contraint par XContainer.

Paramètres

source
IEnumerable<T>

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

Retours

IEnumerable<T> de XElement des éléments enfants de chaque élément ou document de la collection source.

Exemples

L’exemple suivant récupère une collection d’éléments avec le nom d’élément de Child. Il utilise ensuite cette méthode d’axe pour récupérer tous les éléments enfants de la collection.

XElement xmlTree = new XElement("Root",  
    new XElement("Child",  
        new XElement("GrandChild1", 1),  
        new XElement("GrandChild2", 2)  
    ),  
    new XElement("Child",  
        new XElement("GrandChild3", 3),  
        new XElement("GrandChild4", 4)  
    ),  
    new XElement("Child",  
        new XElement("GrandChild5", 5),  
        new XElement("GrandChild6", 6)  
    )  
);  

IEnumerable<XElement> allGrandChildren =  
    from el in xmlTree.Elements("Child").Elements()  
    select el;  

foreach (XElement el in allGrandChildren)  
    Console.WriteLine(el);  
Dim xmlTree As XElement = _  
     <Root>  
          <Child>  
              <GrandChild1>1</GrandChild1>  
              <GrandChild2>2</GrandChild2>  
          </Child>  

          <Child>  
              <GrandChild3>3</GrandChild3>  
              <GrandChild4>4</GrandChild4>  
          </Child>  

          <Child>  
              <GrandChild5>5</GrandChild5>  
              <GrandChild6>6</GrandChild6>  
          </Child>  
      </Root>  

Dim allGrandChildren = From el In xmlTree.<Child>.Elements _  
                       Select el  

For Each el As XElement In allGrandChildren  
    Console.WriteLine(el)  
Next  

Cet exemple produit la sortie suivante :

<GrandChild1>1</GrandChild1>  
<GrandChild2>2</GrandChild2>  
<GrandChild3>3</GrandChild3>  
<GrandChild4>4</GrandChild4>  
<GrandChild5>5</GrandChild5>  
<GrandChild6>6</GrandChild6>  

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 XElement(aw + "Child",  
        new XElement(aw + "GrandChild1", 1),  
        new XElement(aw + "GrandChild2", 2)  
    ),  
    new XElement(aw + "Child",  
        new XElement(aw + "GrandChild3", 3),  
        new XElement(aw + "GrandChild4", 4)  
    ),  
    new XElement(aw + "Child",  
        new XElement(aw + "GrandChild5", 5),  
        new XElement(aw + "GrandChild6", 6)  
    )  
);  

IEnumerable<XElement> allGrandChildren =  
    from el in xmlTree.Elements(aw + "Child").Elements()  
    select el;  

foreach (XElement el in allGrandChildren)  
    Console.WriteLine(el);  
Imports <xmlns="http://www.adventure-works.com">  

Module Module1  
    Sub Main()  
        Dim xmlTree As XElement = _  
             <Root>  
                 <Child>  
                     <GrandChild1>1</GrandChild1>  
                     <GrandChild2>2</GrandChild2>  
                 </Child>  

                 <Child>  
                     <GrandChild3>3</GrandChild3>  
                     <GrandChild4>4</GrandChild4>  
                 </Child>  

                 <Child>  
                     <GrandChild5>5</GrandChild5>  
                     <GrandChild6>6</GrandChild6>  
                 </Child>  
             </Root>  

        Dim allGrandChildren = From el In xmlTree.<Child>.Elements _  
                               Select el  

        For Each el As XElement In allGrandChildren  
            Console.WriteLine(el)  
        Next  
    End Sub  
End Module  

Cet exemple produit la sortie suivante :

<GrandChild1 xmlns="http://www.adventure-works.com">1</GrandChild1>  
<GrandChild2 xmlns="http://www.adventure-works.com">2</GrandChild2>  
<GrandChild3 xmlns="http://www.adventure-works.com">3</GrandChild3>  
<GrandChild4 xmlns="http://www.adventure-works.com">4</GrandChild4>  
<GrandChild5 xmlns="http://www.adventure-works.com">5</GrandChild5>  
<GrandChild6 xmlns="http://www.adventure-works.com">6</GrandChild6>  

Remarques

Bien que Visual Basic contienne un axe d’éléments intégré qui vous permet de rechercher tous les éléments enfants avec un spécifié XName pour chaque élément de la collection source, il n’existe aucun axe d’éléments intégré qui vous permet de récupérer une collection de chaque élément enfant pour chaque élément de la collection source.

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

Voir aussi

S’applique à

Elements<T>(IEnumerable<T>, XName)

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

public:
generic <typename T>
 where T : System::Xml::Linq::XContainer[System::Runtime::CompilerServices::Extension]
 static System::Collections::Generic::IEnumerable<System::Xml::Linq::XElement ^> ^ Elements(System::Collections::Generic::IEnumerable<T> ^ source, System::Xml::Linq::XName ^ name);
public static System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> Elements<T> (this System.Collections.Generic.IEnumerable<T> source, System.Xml.Linq.XName name) where T : System.Xml.Linq.XContainer;
public static System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> Elements<T> (this System.Collections.Generic.IEnumerable<T?> source, System.Xml.Linq.XName? name) where T : System.Xml.Linq.XContainer;
static member Elements : seq<'T (requires 'T :> System.Xml.Linq.XContainer)> * System.Xml.Linq.XName -> seq<System.Xml.Linq.XElement> (requires 'T :> System.Xml.Linq.XContainer)
<Extension()>
Public Function Elements(Of T As XContainer) (source As IEnumerable(Of T), name As XName) As IEnumerable(Of XElement)

Paramètres de type

T

Type des objets de source, contraint par XContainer.

Paramètres

source
IEnumerable<T>

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

name
XName

XName à mettre en correspondance.

Retours

IEnumerable<T> de XElement des éléments enfants de chaque élément et document de la collection source. Seuls les éléments avec un XName correspondant sont inclus dans la collection.

Exemples

Cette méthode d’extension est utile lorsque vous souhaitez récupérer tous les éléments portant un nom spécifié à une profondeur particulière. Cela est facile si le document est très régulier, mais si le document est irrégulier, il peut être un peu plus difficile. Dans l’exemple suivant, nous voulons récupérer tous les aaa éléments qui sont des enfants d’éléments Item . Un élément donné Item peut contenir ou non un aaa élément. Cette opération est facilement effectuée à l’aide de cette méthode d’extension, comme suit :

XElement xmlTree = new XElement("Root",  
    new XElement("Item",  
        new XElement("aaa", 1),  
        new XElement("bbb", 2)  
    ),  
    new XElement("Item",  
        new XElement("ccc", 3),  
        new XElement("aaa", 4)  
    ),  
    new XElement("Item",  
        new XElement("ddd", 5),  
        new XElement("eee", 6)  
    )  
);  

IEnumerable<XElement> allGrandChildren =  
    from el in xmlTree.Elements("Item").Elements("aaa")  
    select el;  

foreach (XElement el in allGrandChildren)  
    Console.WriteLine(el);  
Dim xmlTree As XElement = _  
    <Root>  
        <Item>  
            <aaa>1</aaa>  
            <bbb>2</bbb>  
        </Item>  

        <Item>  
            <ccc>3</ccc>  
            <aaa>4</aaa>  
        </Item>  

        <Item>  
            <ddd>5</ddd>  
            <eee>6</eee>  
        </Item>  
    </Root>  

Dim allGrandChildren = From el In xmlTree.<Item>.<aaa> _  
                       Select el  

For Each el As XElement In allGrandChildren  
    Console.WriteLine(el)  
Next  

Cet exemple produit la sortie suivante :

<aaa>1</aaa>  
<aaa>4</aaa>  

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 XElement(aw + "Item",  
        new XElement(aw + "aaa", 1),  
        new XElement(aw + "bbb", 2)  
    ),  
    new XElement(aw + "Item",  
        new XElement(aw + "ccc", 3),  
        new XElement(aw + "aaa", 4)  
    ),  
    new XElement(aw + "Item",  
        new XElement(aw + "ddd", 5),  
        new XElement(aw + "eee", 6)  
    )  
);  

IEnumerable<XElement> allGrandChildren =  
    from el in xmlTree.Elements(aw + "Item").Elements(aw + "aaa")  
    select el;  

foreach (XElement el in allGrandChildren)  
    Console.WriteLine(el);  
Imports <xmlns="http://www.adventure-works.com">  

Module Module1  
    Sub Main()  
        Dim xmlTree As XElement = _  
            <Root>  
                <Item>  
                    <aaa>1</aaa>  
                    <bbb>2</bbb>  
                </Item>  

                <Item>  
                    <ccc>3</ccc>  
                    <aaa>4</aaa>  
                </Item>  

                <Item>  
                    <ddd>5</ddd>  
                    <eee>6</eee>  
                </Item>  
            </Root>  

        Dim allGrandChildren = From el In xmlTree.<Item>.<aaa> _  
                               Select el  

        For Each el As XElement In allGrandChildren  
            Console.WriteLine(el)  
        Next  
    End Sub  
End Module  

Cet exemple produit la sortie suivante :

<aaa xmlns="http://www.adventure-works.com">1</aaa>  
<aaa xmlns="http://www.adventure-works.com">4</aaa>  

Remarques

Les utilisateurs Visual Basic peuvent utiliser l’axe des éléments intégrés pour récupérer les éléments enfants de chaque élément d’une collection.

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

Voir aussi

S’applique à