XContainer.Elements Metodo

Definizione

Restituisce una raccolta degli elementi figlio dell'elemento o documento nell'ordine dei documenti.

Overload

Elements()

Restituisce una raccolta degli elementi figlio dell'elemento o documento nell'ordine dei documenti.

Elements(XName)

Restituisce una raccolta filtrata degli elementi figlio di questo elemento o documento nell'ordine dei documenti. Solo gli elementi che hanno un oggetto XName corrispondente vengono inclusi nella raccolta.

Commenti

Questo metodo usa l'esecuzione posticipata.

Elements()

Source:
XContainer.cs
Source:
XContainer.cs
Source:
XContainer.cs

Restituisce una raccolta degli elementi figlio dell'elemento o documento nell'ordine dei documenti.

public:
 System::Collections::Generic::IEnumerable<System::Xml::Linq::XElement ^> ^ Elements();
public System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> Elements ();
member this.Elements : unit -> seq<System.Xml.Linq.XElement>
Public Function Elements () As IEnumerable(Of XElement)

Restituisce

IEnumerable<T> di XElement che contiene gli elementi figlio del XContainer nell'ordine dei documenti.

Esempio

Nell'esempio seguente viene creato un albero XML e quindi vengono selezionati alcuni elementi usando questo metodo dell'asse.

XElement xmlTree = new XElement("Root",  
    new XElement("Child1", 1),  
    new XElement("Child2", 2),  
    new XElement("Child3", 3),  
    new XElement("Child4", 4),  
    new XElement("Child5", 5)  
);  
IEnumerable<XElement> elements =  
    from el in xmlTree.Elements()  
    where (int)el <= 3  
    select el;  
foreach (XElement el in elements)  
    Console.WriteLine(el);  
Dim xmlTree As XElement = _  
        <Root>  
            <Child1>1</Child1>  
            <Child2>2</Child2>  
            <Child3>3</Child3>  
            <Child4>4</Child4>  
            <Child5>5</Child5>  
        </Root>  

Dim elements = From el In xmlTree.Elements _  
               Where el.Value <= 3 _  
               Select el  

For Each el In elements  
    Console.WriteLine(el)  
Next  

Nell'esempio viene prodotto l'output seguente:

<Child1>1</Child1>  
<Child2>2</Child2>  
<Child3>3</Child3>  

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 spazi dei nomi 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 XElement(aw + "Child1", 1),  
    new XElement(aw + "Child2", 2),  
    new XElement(aw + "Child3", 3),  
    new XElement(aw + "Child4", 4),  
    new XElement(aw + "Child5", 5)  
);  
IEnumerable<XElement> elements =  
    from el in xmlTree.Elements()  
    where (int)el <= 3  
    select el;  
foreach (XElement el in elements)  
    Console.WriteLine(el);  
Imports <xmlns:aw="http://www.adventure-works.com">  

Module Module1  
    Sub Main()  
        Dim xmlTree As XElement = _  
            <aw:Root>  
                <aw:Child1>1</aw:Child1>  
                <aw:Child2>2</aw:Child2>  
                <aw:Child3>3</aw:Child3>  
                <aw:Child4>4</aw:Child4>  
                <aw:Child5>5</aw:Child5>  
            </aw:Root>  

        Dim elements = From el In xmlTree.Elements _  
                       Where el.Value <= 3 _  
                       Select el  

        For Each el In elements  
            Console.WriteLine(el)  
        Next  
    End Sub  
End Module  

Nell'esempio viene prodotto l'output seguente:

<aw:Child1 xmlns:aw="http://www.adventure-works.com">1</aw:Child1>  
<aw:Child2 xmlns:aw="http://www.adventure-works.com">2</aw:Child2>  
<aw:Child3 xmlns:aw="http://www.adventure-works.com">3</aw:Child3>  

Commenti

Questo metodo usa l'esecuzione posticipata.

Vedi anche

Si applica a

Elements(XName)

Source:
XContainer.cs
Source:
XContainer.cs
Source:
XContainer.cs

Restituisce una raccolta filtrata degli elementi figlio di questo elemento o documento nell'ordine dei documenti. Solo gli elementi che hanno un oggetto XName corrispondente vengono inclusi nella raccolta.

public:
 System::Collections::Generic::IEnumerable<System::Xml::Linq::XElement ^> ^ Elements(System::Xml::Linq::XName ^ name);
public System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> Elements (System.Xml.Linq.XName name);
public System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> Elements (System.Xml.Linq.XName? name);
member this.Elements : System.Xml.Linq.XName -> seq<System.Xml.Linq.XElement>
Public Function Elements (name As XName) As IEnumerable(Of XElement)

Parametri

name
XName

Oggetto XName di cui trovare la corrispondenza.

Restituisce

IEnumerable<T> di XElement che contiene i figli del XContainer che ha un XName corrispondente nell'ordine dei documenti.

Esempio

Nell'esempio seguente viene creato un albero XML e quindi vengono selezionati diversi elementi figlio usando questo metodo dell'asse.

XElement xmlTree = new XElement("Root",  
    new XElement("Type1", 1),  
    new XElement("Type1", 2),  
    new XElement("Type2", 3),  
    new XElement("Type2", 4),  
    new XElement("Type2", 5)  
);  
IEnumerable<XElement> elements =  
    from el in xmlTree.Elements("Type2")  
    select el;  
foreach (XElement el in elements)  
    Console.WriteLine(el);  
Dim xmlTree As XElement = _   
        <Root>  
            <Type1>1</Type1>  
            <Type1>2</Type1>  
            <Type2>3</Type2>  
            <Type2>4</Type2>  
            <Type2>5</Type2>  
        </Root>  

Dim elements = From el In xmlTree.<Type2> _  
               Select el  

For Each el In elements  
    Console.WriteLine(el)  
Next  

Nell'esempio viene prodotto l'output seguente:

<Type2>3</Type2>  
<Type2>4</Type2>  
<Type2>5</Type2>  

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 spazi dei nomi 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 XElement(aw + "Type1", 1),  
    new XElement(aw + "Type1", 2),  
    new XElement(aw + "Type2", 3),  
    new XElement(aw + "Type2", 4),  
    new XElement(aw + "Type2", 5)  
);  
IEnumerable<XElement> elements =  
    from el in xmlTree.Elements(aw + "Type2")  
    select el;  
foreach (XElement el in elements)  
    Console.WriteLine(el);  
Imports <xmlns:aw="http://www.adventure-works.com">  

Module Module1  
    Sub Main()  
        Dim xmlTree As XElement = _   
            <aw:Root>  
                <aw:Type1>1</aw:Type1>  
                <aw:Type1>2</aw:Type1>  
                <aw:Type2>3</aw:Type2>  
                <aw:Type2>4</aw:Type2>  
                <aw:Type2>5</aw:Type2>  
            </aw:Root>  

        Dim elements = From el In xmlTree.<aw:Type2> _  
                       Select el  

        For Each el In elements  
            Console.WriteLine(el)  
        Next  
    End Sub  
End Module  

Nell'esempio viene prodotto l'output seguente:

<aw:Type2 xmlns:aw="http://www.adventure-works.com">3</aw:Type2>  
<aw:Type2 xmlns:aw="http://www.adventure-works.com">4</aw:Type2>  
<aw:Type2 xmlns:aw="http://www.adventure-works.com">5</aw:Type2>  

Commenti

Questo metodo usa l'esecuzione posticipata.

Vedi anche

Si applica a