共用方式為


Extensions.InDocumentOrder<T> Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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)

Syntax

'Declaration
<ExtensionAttribute> _
Public Shared Function InDocumentOrder(Of T As XNode) ( _
    source As IEnumerable(Of T) _
) As IEnumerable(Of T)
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

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.

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.

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.

Dim output As New StringBuilder
Dim xmlTree As XElement = _
    <Root>
        <Item>
            <aaa>1</aaa>
            <bbb>2</bbb>
        </Item>

        <Item>
            <ccc>3</ccc>
            <aaa>4</aaa>
        </Item>

        <Item>
            <ddd>5</ddd>
            <eee>6</eee>
        </Item>
    </Root>

Dim elementList() As XElement = _
    { _
        xmlTree...<ddd>(0), _
        xmlTree...<ccc>(0), _
        xmlTree...<aaa>(0) _
    }

Dim inDocOrder = elementList.InDocumentOrder

For Each el As XElement In inDocOrder
    output.Append(el)
    output.Append(Environment.NewLine)
Next


OutputTextBlock.Text = output.ToString()
    StringBuilder output = new StringBuilder();
    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)
        output.Append(el + Environment.NewLine);

    OutputTextBlock.Text = output.ToString();

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.