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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…