score:1

Accepted answer
public instructionsequence getsequencefromsource(xdocument urisource)
{
    return this.getsequencefromsource(urisource.root);
}

public instructionsequence getsequencefromsource(xelement step)
{
    return new instructionsequence
    {
        step = step.element("position") == null ? 0 : (int)step.element("position"),
        sequence = step.element("sequence")?.elements("step").select(s => this.getsequencefromsource(s)).tolist(),
    };
}

score:0

func<xelement, instructionsequence> getchildcontents = child => new instructionsequence { step = convert.toint32(child.element("position").value), sequence = null };

var matchitems = new list<instructionsequence>();

var splitgroups = doc.root
    .descendants("step")
    .groupby(v => (v.firstnode is xelement) && (((xelement)v.firstnode).name != "sequence"));

    var matchgroup = splitgroups.single(e => e.key == true);
    var remaining = splitgroups.single(e => e.key == false);

    matchitems.addrange(matchgroup.select(getchildcontents));

    matchitems.addrange(remaining.descendants("sequence").descendants("step").select(getchildcontents));

Related Query

More Query from same tag