Extensions.Attributes Metoda

Definicja

Zwraca kolekcję atrybutów każdego elementu w kolekcji źródłowej.

Przeciążenia

Attributes(IEnumerable<XElement>)

Zwraca kolekcję atrybutów każdego elementu w kolekcji źródłowej.

Attributes(IEnumerable<XElement>, XName)

Zwraca filtrowaną kolekcję atrybutów każdego elementu w kolekcji źródłowej. W kolekcji znajdują się tylko elementy, które mają dopasowanie XName .

Uwagi

Visual Basic użytkownicy mogą używać zintegrowanej osi atrybutów do pobierania atrybutów o określonej nazwie z kolekcji elementów.

Ta metoda używa odroczonego wykonania.

Attributes(IEnumerable<XElement>)

Zwraca kolekcję atrybutów każdego elementu w kolekcji źródłowej.

public:
[System::Runtime::CompilerServices::Extension]
 static System::Collections::Generic::IEnumerable<System::Xml::Linq::XAttribute ^> ^ Attributes(System::Collections::Generic::IEnumerable<System::Xml::Linq::XElement ^> ^ source);
public static System.Collections.Generic.IEnumerable<System.Xml.Linq.XAttribute> Attributes (this System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> source);
public static System.Collections.Generic.IEnumerable<System.Xml.Linq.XAttribute> Attributes (this System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement?> source);
static member Attributes : seq<System.Xml.Linq.XElement> -> seq<System.Xml.Linq.XAttribute>
<Extension()>
Public Function Attributes (source As IEnumerable(Of XElement)) As IEnumerable(Of XAttribute)

Parametry

source
IEnumerable<XElement>

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

Zwraca

IEnumerable<XAttribute>

XAttribute Element IEnumerable<T> zawierający atrybuty każdego elementu w kolekcji źródłowej.

Przykłady

Poniższy przykład pobiera kolekcję elementów, a następnie pobiera kolekcję wszystkich atrybutów wszystkich elementów w kolekcji. Należy pamiętać, że wynikowa kolekcja zawiera tylko atrybuty Child1 elementów i Child2 , a nie atrybuty Root elementu.

Należy pamiętać, że atrybut przestrzeni nazw jest zwracany przez tę metodę.

XElement xmlTree = new XElement("Root",  
    new XAttribute(XNamespace.Xmlns + "aw", "http://www.adventure-works.com"),  
    new XAttribute("Att1", "content1"),  
    new XAttribute("Att2", "content2"),  
    new XElement("Child1",  
        new XAttribute("Att1", "content3"),  
        new XAttribute("Att2", "content4")  
    ),  
    new XElement("Child2",  
        new XAttribute("Att1", "content5"),  
        new XAttribute("Att2", "content6")  
    )  
);  
Console.WriteLine(xmlTree);  
Console.WriteLine("-----");  

IEnumerable<XAttribute> attList =  
    from att in xmlTree.DescendantsAndSelf().Attributes()  
    select att;  

foreach (XAttribute att in attList)  
    Console.WriteLine(att);  
Dim xmlTree As XElement = _  
    <Root xmlns:aw="http://www.adventure-works.com" Att1="content1" Att2="content2">  
        <Child1 Att1="content3" Att2="content4"/>  
        <Child2 Att1="content5" Att2="content6"/>  
    </Root>  

Dim attList = _  
    From att In xmlTree.DescendantsAndSelf.Attributes _  
    Select att  

Console.WriteLine(xmlTree)  
Console.WriteLine("-----")  

For Each att As XAttribute In attList  
    Console.WriteLine(att)  
Next  

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

<Root xmlns:aw="http://www.adventure-works.com" Att1="content1" Att2="content2">  
  <Child1 Att1="content3" Att2="content4" />  
  <Child2 Att1="content5" Att2="content6" />  
</Root>  
-----  
xmlns:aw="http://www.adventure-works.com"  
Att1="content1"  
Att2="content2"  
Att1="content3"  
Att2="content4"  
Att1="content5"  
Att2="content6"  

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. Należy pamiętać, że atrybut przestrzeni nazw jest uwzględniony w zwróconej kolekcji.

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"),  
    new XElement(aw + "Child1",  
        new XAttribute(aw + "Att1", "content3"),  
        new XAttribute(aw + "Att2", "content4")  
    ),  
    new XElement(aw + "Child2",  
        new XAttribute(aw + "Att1", "content5"),  
        new XAttribute(aw + "Att2", "content6")  
    )  
);  
Console.WriteLine(xmlTree);  
Console.WriteLine("-----");  

IEnumerable<XAttribute> attList =  
    from att in xmlTree.DescendantsAndSelf().Attributes()  
    select att;  

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 xmlns:aw="http://www.adventure-works.com" aw:Att1="content1" aw:Att2="content2">  
                <aw:Child1 aw:Att1="content3" aw:Att2="content4"/>  
                <aw:Child2 aw:Att1="content5" aw:Att2="content6"/>  
            </aw:Root>  

        Dim attList = _  
            From att In xmlTree.DescendantsAndSelf.Attributes _  
            Select att  

        Console.WriteLine(xmlTree)  
        Console.WriteLine("-----")  

        For Each att As XAttribute In attList  
            Console.WriteLine(att)  
        Next  
    End Sub  
End Module  

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

<aw:Root xmlns:aw="http://www.adventure-works.com" aw:Att1="content1" aw:Att2="content2">  
  <aw:Child1 aw:Att1="content3" aw:Att2="content4" />  
  <aw:Child2 aw:Att1="content5" aw:Att2="content6" />  
</aw:Root>  
-----  
xmlns:aw="http://www.adventure-works.com"  
aw:Att1="content1"  
aw:Att2="content2"  
aw:Att1="content3"  
aw:Att2="content4"  
aw:Att1="content5"  
aw:Att2="content6"  

Uwagi

Należy pamiętać, że w przeciwieństwie do innych interfejsów programowania XML w LINQ to XML przestrzenie nazw są wyświetlane jako atrybuty.

Chociaż Visual Basic użytkownicy mogą używać zintegrowanej osi atrybutów do pobierania atrybutów o określonej nazwie z kolekcji elementów, nie ma zintegrowanej osi Visual Basic w celu pobrania wszystkich atrybutów wszystkich elementów w kolekcji.

Ta metoda używa odroczonego wykonania.

Zobacz też

Dotyczy

Attributes(IEnumerable<XElement>, XName)

Zwraca filtrowaną kolekcję atrybutów każdego elementu w kolekcji źródłowej. W kolekcji znajdują się tylko elementy, które mają dopasowanie XName .

public:
[System::Runtime::CompilerServices::Extension]
 static System::Collections::Generic::IEnumerable<System::Xml::Linq::XAttribute ^> ^ Attributes(System::Collections::Generic::IEnumerable<System::Xml::Linq::XElement ^> ^ source, System::Xml::Linq::XName ^ name);
public static System.Collections.Generic.IEnumerable<System.Xml.Linq.XAttribute> Attributes (this System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> source, System.Xml.Linq.XName name);
public static System.Collections.Generic.IEnumerable<System.Xml.Linq.XAttribute> Attributes (this System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement?> source, System.Xml.Linq.XName? name);
static member Attributes : seq<System.Xml.Linq.XElement> * System.Xml.Linq.XName -> seq<System.Xml.Linq.XAttribute>
<Extension()>
Public Function Attributes (source As IEnumerable(Of XElement), name As XName) As IEnumerable(Of XAttribute)

Parametry

source
IEnumerable<XElement>

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

name
XName

Element XName do dopasowania.

Zwraca

IEnumerable<XAttribute>

XAttribute Element IEnumerable<T> zawierający filtrowaną kolekcję atrybutów każdego elementu 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, która w tym przypadku zawiera Child1 elementy i Child2 . Następnie pobiera wszystkie atrybuty tej kolekcji podrzędnej o nazwie Att1.

XElement xmlTree = new XElement("Root",  
    new XAttribute("Att1", "content1"),  
    new XAttribute("Att2", "content2"),  
    new XElement("Child1",  
        new XAttribute("Att1", "content3"),  
        new XAttribute("Att2", "content4")  
    ),  
    new XElement("Child2",  
        new XAttribute("Att1", "content5"),  
        new XAttribute("Att2", "content6")  
    )  
);  

IEnumerable<XAttribute> attList = from att in xmlTree.Elements().Attributes("Att1")  
                                  select att;  

foreach (XAttribute att in attList)  
    Console.WriteLine(att);  
Dim xmlTree As XElement = _  
    <Root Att1="content1" Att2="content2">  
        <Child1 Att1="content3" Att2="content4">  
        </Child1>  
        <Child2 Att1="content5" Att2="content6">  
        </Child2>  
    </Root>  

Dim attList = From att In xmlTree.Elements.Attributes("Att1") _  
                          Select att  

For Each att As XAttribute In attList  
    Console.WriteLine(att)  
Next  

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

Att1="content3"  
Att1="content5"  

Uwagi

Należy pamiętać, że w przeciwieństwie do innych interfejsów programowania XML, w LINQ to XML przestrzenie nazw są udostępniane jako atrybuty.

Ta metoda używa odroczonego wykonania.

Zobacz też

Dotyczy