XNode.ElementsBeforeSelf Method

Definition

Returns a collection of the sibling elements before this node, in document order.

Overloads

ElementsBeforeSelf()

Returns a collection of the sibling elements before this node, in document order.

ElementsBeforeSelf(XName)

Returns a filtered collection of the sibling elements before this node, in document order. Only elements that have a matching XName are included in the collection.

Remarks

This method uses deferred execution.

ElementsBeforeSelf()

Source:
XNode.cs
Source:
XNode.cs
Source:
XNode.cs

Returns a collection of the sibling elements before this node, in document order.

C#
public System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> ElementsBeforeSelf();

Returns

An IEnumerable<T> of XElement of the sibling elements before this node, in document order.

Examples

The following example uses this axis method.

C#
XElement xmlTree = new XElement("Root",  
    new XText("Text content."),  
    new XElement("Child1", "child1 content"),  
    new XElement("Child2", "child2 content"),  
    new XElement("Child3", "child3 content"),  
    new XText("More text content."),  
    new XElement("Child4", "child4 content"),  
    new XElement("Child5", "child5 content")  
);  
XElement child = xmlTree.Element("Child3");  
IEnumerable<XElement> elements = child.ElementsBeforeSelf();  
foreach (XElement el in elements)  
    Console.WriteLine(el.Name);  

This example produces the following output:

Child1  
Child2  

Remarks

This method only includes siblings in the returned collection. It does not include descendants.

This method uses deferred execution.

See also

Applies to

.NET 10 and other versions
Product Versions
.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, 8, 9, 10
.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, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

ElementsBeforeSelf(XName)

Source:
XNode.cs
Source:
XNode.cs
Source:
XNode.cs

Returns a filtered collection of the sibling elements before this node, in document order. Only elements that have a matching XName are included in the collection.

C#
public System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> ElementsBeforeSelf(System.Xml.Linq.XName name);
C#
public System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> ElementsBeforeSelf(System.Xml.Linq.XName? name);

Parameters

name
XName

The XName to match.

Returns

An IEnumerable<T> of XElement of the sibling elements before this node, in document order. Only elements that have a matching XName are included in the collection.

Examples

The following example uses this method.

C#
XElement xmlTree = new XElement("Root",  
    new XText("Text content."),  
    new XElement("Child1", "child1 content"),  
    new XElement("Child2", "child2 content"),  
    new XElement("Child3", "child3 content"),  
    new XText("More text content."),  
    new XElement("Child4", "child4 content"),  
    new XElement("Child5", "child5 content")  
);  
XElement child = xmlTree.Element("Child3");  
IEnumerable<XElement> elements = child.ElementsBeforeSelf("Child2");  
foreach (XElement el in elements)  
    Console.WriteLine(el.Name);  

This example produces the following output:

Child2  

Remarks

This method only includes siblings in the returned collection. It does not include descendants.

This method uses deferred execution.

See also

Applies to

.NET 10 and other versions
Product Versions
.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, 8, 9, 10
.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, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0