Extensions.Ancestors Metoda

Definicja

Zwraca kolekcję elementów, które zawierają elementy główne każdego węzła w kolekcji źródłowej.

Przeciążenia

Ancestors<T>(IEnumerable<T>)

Zwraca kolekcję elementów, które zawierają elementy główne każdego węzła w kolekcji źródłowej.

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

Zwraca filtrowaną kolekcję elementów, które zawierają elementy główne każdego węzła w kolekcji źródłowej. W kolekcji znajdują się tylko elementy, które mają dopasowanie XName .

Uwagi

Jeśli wiele węzłów w kolekcji źródłowej ma ten sam element nadrzędny, program ancestor zostanie uwzględniony wiele razy w kolekcji wyników. Aby tego uniknąć, użyj Distinct metody .

Ta metoda używa odroczonego wykonania.

Ancestors<T>(IEnumerable<T>)

Zwraca kolekcję elementów, które zawierają elementy główne każdego węzła w kolekcji źródłowej.

C#
public static System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> Ancestors<T> (this System.Collections.Generic.IEnumerable<T> source) where T : System.Xml.Linq.XNode;
C#
public static System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> Ancestors<T> (this System.Collections.Generic.IEnumerable<T?> source) where T : System.Xml.Linq.XNode;

Parametry typu

T

Typ obiektów w elemecie source, ograniczony do XNode.

Parametry

source
IEnumerable<T>

Element IEnumerable<T> zawierający XNode kolekcję źródłową.

Zwraca

IEnumerable<XElement>

XElement Element, który IEnumerable<T> zawiera elementy główne każdego węzła w kolekcji źródłowej.

Przykłady

Poniższy przykład pobiera kolekcję elementów wnuków. Następnie używa tej metody osi, aby pobrać wszystkie elementy podrzędne wszystkich elementów w kolekcji.

C#
XElement xmlTree = new XElement("Root",  
    new XElement("Child1",  
        new XElement("GrandChild1",  
            new XElement("GreatGrandChild1", "content")  
        )  
    ),  
    new XElement("Child2",  
        new XElement("GrandChild2",  
            new XElement("GreatGrandChild2", "content")  
        )  
    )  
);  
IEnumerable<XElement> greatGrandChildren =  
    from el in xmlTree.Descendants()  
    where el.Name.LocalName.StartsWith("Great")  
    select el;  

Console.WriteLine("Great Grand Children Elements");  
Console.WriteLine("----");  
foreach (XElement de in greatGrandChildren)  
    Console.WriteLine(de.Name);  

IEnumerable<XElement> allAncestors =  
    from el in greatGrandChildren.Ancestors().Distinct()  
    select el;  

Console.WriteLine("");  
Console.WriteLine("Ancestors");  
Console.WriteLine("----");  
foreach (XElement de in allAncestors)  
    Console.WriteLine(de.Name);  

Ten przykład generuje następujące wyniki:

Great Grand Children Elements  
----  
GreatGrandChild1  
GreatGrandChild2  

Ancestors  
----  
GrandChild1  
Child1  
Root  
GrandChild2  
Child2  

Poniżej przedstawiono ten sam przykład, ale w tym przypadku kod XML znajduje się w przestrzeni nazw. Aby uzyskać więcej informacji, zobacz Praca z przestrzeniami nazw XML.

C#
XNamespace aw = "http://www.adventure-works.com";  
XElement xmlTree = new XElement(aw + "Root",  
    new XElement(aw + "Child1",  
        new XElement(aw + "GrandChild1",  
            new XElement(aw + "GreatGrandChild1", "content")  
        )  
    ),  
    new XElement(aw + "Child2",  
        new XElement(aw + "GrandChild2",  
            new XElement(aw + "GreatGrandChild2", "content")  
        )  
    )  
);  
IEnumerable<XElement> greatGrandChildren =  
    from el in xmlTree.Descendants()  
    where el.Name.LocalName.StartsWith("Great")  
    select el;  

Console.WriteLine("Great Grand Children Elements");  
Console.WriteLine("----");  
foreach (XElement de in greatGrandChildren)  
    Console.WriteLine(de.Name);  

IEnumerable<XElement> allAncestors =  
    from el in greatGrandChildren.Ancestors().Distinct()  
    select el;  

Console.WriteLine("");  
Console.WriteLine("Ancestors");  
Console.WriteLine("----");  
foreach (XElement de in allAncestors)  
    Console.WriteLine(de.Name);  

Ten przykład generuje następujące wyniki:

Great Grand Children Elements  
----  
{http://www.adventure-works.com}GreatGrandChild1  
{http://www.adventure-works.com}GreatGrandChild2  

Ancestors  
----  
{http://www.adventure-works.com}GrandChild1  
{http://www.adventure-works.com}Child1  
{http://www.adventure-works.com}Root  
{http://www.adventure-works.com}GrandChild2  
{http://www.adventure-works.com}Child2  

Uwagi

Jeśli wiele węzłów w kolekcji źródłowej ma ten sam element nadrzędny, program ancestor zostanie uwzględniony wiele razy w kolekcji wyników. Aby tego uniknąć, użyj Distinct metody .

Ta metoda używa odroczonego wykonania.

Zobacz też

Dotyczy

.NET 7 i inne wersje
Produkt Wersje
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

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

Zwraca filtrowaną kolekcję elementów, które zawierają elementy główne każdego węzła w kolekcji źródłowej. W kolekcji znajdują się tylko elementy, które mają dopasowanie XName .

C#
public static System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> Ancestors<T> (this System.Collections.Generic.IEnumerable<T> source, System.Xml.Linq.XName name) where T : System.Xml.Linq.XNode;
C#
public static System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> Ancestors<T> (this System.Collections.Generic.IEnumerable<T?> source, System.Xml.Linq.XName? name) where T : System.Xml.Linq.XNode;

Parametry typu

T

Typ obiektów w elemecie source, ograniczony do XNode.

Parametry

source
IEnumerable<T>

Element IEnumerable<T> zawierający XNode kolekcję źródłową.

name
XName

Element XName do dopasowania.

Zwraca

IEnumerable<XElement>

XElement Element, który IEnumerable<T> zawiera elementy główne każdego węzła w kolekcji źródłowej. W kolekcji znajdują się tylko elementy, które mają dopasowanie XName .

Przykłady

Poniższy przykład pobiera kolekcję elementów wnuków. Następnie używa tej metody osi, aby pobrać wszystkie elementy podrzędne wszystkich elementów w kolekcji zgodnej z określoną XNamewartością .

C#
XElement xmlTree = new XElement("Root",  
    new XElement("Child1",  
        new XElement("GrandChild1",  
            new XElement("GreatGrandChild1", "content")  
        )  
    ),  
    new XElement("Child2",  
        new XElement("GrandChild2",  
            new XElement("GreatGrandChild2", "content")  
        )  
    )  
);  
IEnumerable<XElement> greatGrandChildren =  
    from el in xmlTree.Descendants()  
    where el.Name.LocalName.StartsWith("Great")  
    select el;  

Console.WriteLine("Great Grand Children Elements");  
Console.WriteLine("----");  
foreach (XElement de in greatGrandChildren)  
    Console.WriteLine(de.Name);  

IEnumerable<XElement> allAncestors =  
    from el in greatGrandChildren.Ancestors("Child1")  
    select el;  

Console.WriteLine("");  
Console.WriteLine("Ancestors");  
Console.WriteLine("----");  
foreach (XElement de in allAncestors)  
    Console.WriteLine(de.Name);  

Ten przykład generuje następujące wyniki:

Great Grand Children Elements  
----  
GreatGrandChild1  
GreatGrandChild2  

Ancestors  
----  
Child1  

Poniżej przedstawiono ten sam przykład, ale w tym przypadku kod XML znajduje się w przestrzeni nazw. Aby uzyskać więcej informacji, zobacz Praca z przestrzeniami nazw XML.

C#
XNamespace aw = "http://www.adventure-works.com";  
XElement xmlTree = new XElement(aw + "Root",  
    new XElement(aw + "Child1",  
        new XElement(aw + "GrandChild1",  
            new XElement(aw + "GreatGrandChild1", "content")  
        )  
    ),  
    new XElement(aw + "Child2",  
        new XElement(aw + "GrandChild2",  
            new XElement(aw + "GreatGrandChild2", "content")  
        )  
    )  
);  
IEnumerable<XElement> greatGrandChildren =  
    from el in xmlTree.Descendants()  
    where el.Name.LocalName.StartsWith("Great")  
    select el;  

Console.WriteLine("Great Grand Children Elements");  
Console.WriteLine("----");  
foreach (XElement de in greatGrandChildren)  
    Console.WriteLine(de.Name);  

IEnumerable<XElement> allAncestors =  
    from el in greatGrandChildren.Ancestors(aw + "Child1")  
    select el;  

Console.WriteLine("");  
Console.WriteLine("Ancestors");  
Console.WriteLine("----");  
foreach (XElement de in allAncestors)  
    Console.WriteLine(de.Name);  

Ten przykład generuje następujące wyniki:

Great Grand Children Elements  
----  
{http://www.adventure-works.com}GreatGrandChild1  
{http://www.adventure-works.com}GreatGrandChild2  

Ancestors  
----  
{http://www.adventure-works.com}Child1  

Uwagi

Jeśli wiele węzłów w kolekcji źródłowej ma ten sam element nadrzędny z pasującym XNameelementem, program ancestor zostanie uwzględniony wiele razy w kolekcji wyników.

Ta metoda używa odroczonego wykonania.

Zobacz też

Dotyczy

.NET 7 i inne wersje
Produkt Wersje
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0