Export (0) Print
Expand All

Extensions.InDocumentOrder<T> Method

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

Namespace:  System.Xml.Linq
Assembly:  System.Xml.Linq (in System.Xml.Linq.dll)

public static IEnumerable<T> InDocumentOrder<T>(
	this IEnumerable<T> source
)
where T : XNode

Type Parameters

T

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

Parameters

source
Type: System.Collections.Generic.IEnumerable<T>
An IEnumerable<T> of XNode that contains the source collection.

Return Value

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

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type IEnumerable<T>. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).

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.

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<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>

.NET Framework

Supported in: 4, 3.5

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

ADD
Show:
© 2014 Microsoft