XElement.Attributes Metoda

Definice

Vrátí kolekci atributů tohoto prvku.

Přetížení

Attributes()

Vrátí kolekci atributů tohoto prvku.

Attributes(XName)

Vrátí filtrovanou kolekci atributů tohoto prvku. V kolekci jsou zahrnuty pouze atributy, které mají shodu XName .

Poznámky

Tato metoda používá odložené spuštění.

Attributes()

Vrátí kolekci atributů tohoto prvku.

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

Návraty

IEnumerable<XAttribute>

XAttribute Atribut IEnumerable<T> tohoto prvku.

Příklady

Následující příklad vytvoří prvek se dvěma atributy. Použije ho k načtení všech atributů elementu.

XElement xmlTree = new XElement("Root",  
    new XAttribute("Att1", "content1"),  
    new XAttribute("Att2", "content2")  
);  
IEnumerable<XAttribute> attList =  
    from at in xmlTree.Attributes()  
    select at;  
foreach (XAttribute att in attList)  
    Console.WriteLine(att);  
Dim xmlTree As XElement = <Root Att1="content1" Att2="content2"/>  

Dim attList As IEnumerable(Of XAttribute) = _  
From at In xmlTree.Attributes() _  
Select at  

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

Tento příklad vytvoří následující výstup:

Att1="content1"  
Att2="content2"  

Následující příklad je stejný, ale v tomto případě je XML v oboru názvů. Další informace naleznete v tématu Práce s obory názvů XML.

XNamespace aw = "http://www.adventure-works.com";  
XElement xmlTree = new XElement(aw + "Root",  
    new XAttribute(aw + "Att1", "content1"),  
    new XAttribute(aw + "Att2", "content2"),  
    new XAttribute(XNamespace.Xmlns + "aw", "http://www.adventure-works.com")  
);  
IEnumerable<XAttribute> attList =  
    from at in xmlTree.Attributes()  
    select at;  
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 aw:Att1="content1" aw:Att2="content2"/>  

        Dim attList As IEnumerable(Of XAttribute) = _  
            From at In xmlTree.Attributes() _  
            Select at  

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

Tento příklad vytvoří následující výstup:

aw:Att1="content1"  
aw:Att2="content2"  
xmlns:aw="http://www.adventure-works.com"  

Poznámky

Atributy v vrácené kolekci jsou v pořadí, v jakém byly přidány do elementu. Pokud byl strom XML analyzován z XML, vrátí se atributy v pořadí dokumentu.

Tato metoda používá odložené spuštění.

Viz také

Platí pro

Attributes(XName)

Vrátí filtrovanou kolekci atributů tohoto prvku. V kolekci jsou zahrnuty pouze atributy, které mají shodu XName .

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

Parametry

name
XName

To XName se má shodovat.

Návraty

IEnumerable<XAttribute>

Objekt IEnumerable<T> XAttribute , který obsahuje atributy tohoto prvku. V kolekci jsou zahrnuty pouze atributy, které mají shodu XName .

Příklady

Následující příklad používá tento .

XElement xmlTree = new XElement("Root",  
    new XAttribute("Att1", "content1"),  
    new XAttribute("Att2", "content2")  
);  
IEnumerable<XAttribute> attList = xmlTree.Attributes("Att1");  
foreach (XAttribute att in attList)  
    Console.WriteLine(att);  
Dim xmlTree As XElement = <Root Att1="content1" Att2="content2"/>  

Dim attList As IEnumerable(Of XAttribute) = xmlTree.Attributes("Att1")  

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

Tento příklad vytvoří následující výstup:

Att1="content1"  

Následující příklad je stejný, ale v tomto případě je XML v oboru názvů. Další informace naleznete v tématu Práce s obory názvů 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 XAttribute(aw + "Att1", "content1"),  
    new XAttribute(aw + "Att2", "content2")  
);  
IEnumerable<XAttribute> attList = xmlTree.Attributes(aw + "Att1");  
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 aw:Att1="content1" aw:Att2="content2"/>  

        Dim attList As IEnumerable(Of XAttribute) = xmlTree.Attributes(GetXmlNamespace(aw) + "Att1")  

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

Tento příklad vytvoří následující výstup:

aw:Att1="content1"  

Poznámky

Názvy atributů musí být jedinečné v rámci elementu. To proto může vrátit buď kolekci, která obsahuje pouze jeden atribut, nebo může vrátit prázdnou kolekci.

Tato metoda používá odložené spuštění.

Viz také

Platí pro