排序使用以下:
var xml= xDoc.Element("Root")
.Elements("I")
.OrderByDescending(s => (int) s.Attribute("aa"));
然后保存它:
XDocument doc = new XDocument(new XElement("Root", xml));
doc.Save("C:\\Something.xml");
更新
您可以为此使用 XSLT:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/*">
<xsl:copy>
<xsl:for-each select="I">
<xsl:sort select="@aa" order="ascending"/>
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
并调用它(引用How to apply an XSLT Stylesheet in C#):
XPathDocument myXPathDoc = new XPathDocument(myXmlFile) ;
XslTransform myXslTrans = new XslTransform() ;
myXslTrans.Load(myStyleSheet);
XmlTextWriter myWriter = new XmlTextWriter("result.xml",null) ;
myXslTrans.Transform(myXPathDoc,null,myWriter) ;