Extensions.Elements Metodo

Definizione

Restituisce una raccolta di elementi figlio di ciascun elemento e documento nella raccolta di origine.

Overload

Elements<T>(IEnumerable<T>)

Restituisce una raccolta di elementi figlio di ciascun elemento e documento nella raccolta di origine.

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

Restituisce una raccolta filtrata degli elementi figlio di ciascun elemento e documento nella raccolta di origine. Solo gli elementi che hanno un oggetto XName corrispondente vengono inclusi nella raccolta.

Commenti

Visual Basic contiene un asse di elementi integrati che consente di trovare tutti gli elementi figlio con un elemento specificato XName per ogni elemento della raccolta di origine.

Questo metodo usa l'esecuzione posticipata.

Elements<T>(IEnumerable<T>)

Restituisce una raccolta di elementi figlio di ciascun elemento e documento nella raccolta di origine.

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)

Parametri di tipo

T

Tipo di oggetti in source vincolati a XContainer.

Parametri

source
IEnumerable<T>

IEnumerable<T> di XElement che contiene la raccolta di origine.

Restituisce

IEnumerable<T> di XElement degli elementi figlio di ciascun elemento o documento nella raccolta di origine.

Esempio

Nell'esempio seguente viene recuperata una raccolta di elementi con il nome dell'elemento di Child. Usa quindi questo metodo asse per recuperare tutti gli elementi figlio della raccolta.

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  

Nell'esempio viene prodotto l'output seguente:

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

Di seguito è riportato lo stesso esempio, ma in questo caso il codice XML si trova in uno spazio dei nomi. Per altre informazioni, vedere Usare gli spazi dei nomi 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  

Nell'esempio viene prodotto l'output seguente:

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

Commenti

Anche se Visual Basic contiene un asse di elementi integrati che consente di trovare tutti gli elementi figlio con un elemento specificato XName per ogni elemento nell'insieme di origine, non esiste un asse di elementi integrati che consente di recuperare una raccolta di ogni elemento figlio per ogni elemento figlio nell'insieme di origine.

Questo metodo usa l'esecuzione posticipata.

Vedi anche

Si applica a

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

Restituisce una raccolta filtrata degli elementi figlio di ciascun elemento e documento nella raccolta di origine. Solo gli elementi che hanno un oggetto XName corrispondente vengono inclusi nella raccolta.

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)

Parametri di tipo

T

Tipo di oggetti in source vincolati a XContainer.

Parametri

source
IEnumerable<T>

IEnumerable<T> di XElement che contiene la raccolta di origine.

name
XName

Oggetto XName di cui trovare la corrispondenza.

Restituisce

IEnumerable<T> di XElement degli elementi figlio di ciascun elemento e documento nella raccolta di origine. Solo gli elementi che hanno un oggetto XName corrispondente vengono inclusi nella raccolta.

Esempio

Questo metodo di estensione è utile quando si desidera recuperare tutti gli elementi con un nome specificato in una particolare profondità. Questo è facile se il documento è molto normale, ma se il documento è irregolare, può essere un po 'più difficile. Nell'esempio seguente si desidera recuperare tutti gli aaa elementi figlio di Item elementi. Un determinato Item elemento può o non contenere un aaa elemento. Questa operazione viene eseguita facilmente usando questo metodo di estensione, come indicato di seguito:

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  

Nell'esempio viene prodotto l'output seguente:

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

Di seguito è riportato lo stesso esempio, ma in questo caso il codice XML si trova in uno spazio dei nomi. Per altre informazioni, vedere Usare gli spazi dei nomi 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  

Nell'esempio viene prodotto l'output seguente:

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

Commenti

Gli utenti di Visual Basic possono usare l'asse degli elementi integrati per recuperare gli elementi figlio di ogni elemento in una raccolta.

Questo metodo usa l'esecuzione posticipata.

Vedi anche

Si applica a