Extensions.InDocumentOrder<T>(IEnumerable<T>) Method

Definition

Returns a collection of nodes that contains all nodes in the source collection, sorted in document order.

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

Type Parameters

T

The type of the objects in source, constrained to XNode.

Parameters

source
IEnumerable<T>

An IEnumerable<T> of XNode that contains the source collection.

Returns

An IEnumerable<T> of XNode that contains all nodes in the source collection, sorted in document order.

Examples

The following example creates a collection of nodes that are not in document order, and then uses this axis to create a new collection where the nodes are in document order.

C#
XElement xmlTree = new XElement("Root",  
    new XElement("Item",  
        new XElement("aaa", 1),  
        new XElement("bbb", 2)  
    ),  
    new XElement("Item",  
        new XElement("ccc", 3),  
        new XElement("aaa", 4)  
    ),  
    new XElement("Item",  
        new XElement("ddd", 5),  
        new XElement("eee", 6)  
    )  
);  

XElement[] elementList = {  
    xmlTree.Descendants("ddd").First(),  
    xmlTree.Descendants("ccc").First(),  
    xmlTree.Descendants("aaa").First()  
};  

IEnumerable<XElement> inDocOrder = elementList.InDocumentOrder();  

foreach (XElement el in inDocOrder)  
    Console.WriteLine(el);  

This example produces the following output:

<aaa>1</aaa>  
<ccc>3</ccc>  
<ddd>5</ddd>  

Remarks

This axis method uses deferred execution. However, it first enumerates its source collection, the sorts the nodes in document order, and then yields the results.

Applies to

Ürün Sürümler
.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
.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

See also