Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I can't seem to figure out how to do this. There are several other examples on here, but nothing really matches what I want to do:

Consider the following XMLDocument object:

<Policy>
    <Covers>
        <MyCover1>
            <properties>
                <sortOrder>1</sortOrder>
            </properties>
        </MyCover1>

        <MyCover3>
            <properties>
                <sortOrder>3</sortOrder>
            </properties>
        </MyCover3>

        <MyCover2>
            <properties>
                <sortOrder>2</sortOrder>
            </properties>
        </MyCover2>
    </Covers>
</Policy>

How would I go about in sorting this document based on node "sortOrder" using linQ or another method?

After the sort the outerxml should basically look like this:

<Policy>
    <Covers>
        <MyCover1/>
        <MyCover2/>
        <MyCover3/>
    </Covers>
</Policy>

UPDATE

I've made some progress, the data is now sorted, but how do I update the original unsorted XmlDocument? This is what I have so far:

private static void DoSort(XmlDocument policyDocument)
{
    foreach(XmlNode coverGroup in policyDocument.SelectNodes("//CoverGroup"))
    {
        XDocument test = XDocument.Parse(coverGroup.OuterXml);
        var sorted = from xe in test.Element("CoverGroup").Elements()
             let so = xe.Element("properties").Element("displayOrder")
             let num = (int)so
             orderby num
             select xe;     

        var result = new XElement("CoverGroup", sorted);
    }
}

I need to apply the changes back to "policyDocument". Note: A cover can have a CoverGroup of it's own, which can then again have Covers with CoverGroups of it's own. This goes down at least 4 levels: ie

<Policy>
<Covers>
    <MyCover1>
        <properties>
            <sortOrder></sortOrder>
        </properties>
        <CoverGroup>
            <MyCover1Child>
                <properties>
                    <sortOrder></sortOrder>
                </properties>
            </MyCover1Child>
        </CoverGroup>
    </MyCover1>
    ...
</Covers>

The XPATH and foreach above cathers for the above structure, so I was thinking of maybe just replacing the coverGroup XmlNode in the first foreach with the new sorted list, but I dont know how to reconstruct a new XmlNode. IF we can figure this out, then we can simply do this:

policyDocument.ReplaceChild(coverGroup, mySortedXmlNode)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
273 views
Welcome To Ask or Share your Answers For Others

1 Answer

You may try as follows (using XDocument, not XmlDocument):

var document = XDocument.Parse( /* the xml string */ );

var sorted = from xe in document.Element("Policy").Element("Covers").Elements()
             let so = xe.Element("properties").Element("sortOrder")
             let num = (int)so
             orderby num
             select xe;

var result = new XElement("Policy", new XElement("Covers", sorted));

I write the code without testing it, but I think should be a good starting point.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...