Language

Extensions.InDocumentOrder(IEnumerable) Method

Definition

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

public:
generic 
 where T : System::Xml::Linq::XNode[System::Runtime::CompilerServices::Extension]
 static System::Collections::Generic::IEnumerable ^ InDocumentOrder(System::Collections::Generic::IEnumerable ^ source);
public static System.Collections.Generic.IEnumerable InDocumentOrder(this System.Collections.Generic.IEnumerable source) where T : System.Xml.Linq.XNode;
static member InDocumentOrder : seq<'T (requires 'T :> System.Xml.Linq.XNode)> -> seq<'T (requires 'T :> System.Xml.Linq.XNode)> (requires 'T :> System.Xml.Linq.XNode)

Public Function InDocumentOrder(Of T As XNode) (source As IEnumerable(Of T)) As IEnumerable(Of T)

Type Parameters

T

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

Parameters

source
IEnumerable

An IEnumerable of XNode that contains the source collection.

Returns

An IEnumerable 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.

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 inDocOrder = elementList.InDocumentOrder();

foreach (XElement el in inDocOrder)
    Console.WriteLine(el);
Dim xmlTree As XElement = _
    
        
            1
            2
        

        
            3
            4
        

        
            5
            6
        
    

Dim elementList() As XElement = _
    { _
        xmlTree...(0), _
        xmlTree...(0), _
        xmlTree...(0) _
    }

Dim inDocOrder = elementList.InDocumentOrder

For Each el As XElement In inDocOrder
    Console.WriteLine(el)
Next

This example produces the following output:

1
3
5

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

See also