I just did this. I needed to pass it on to another function that was expecting a xml object so I did the following this is c# code though.
public static XElement xelmSort(this XElement xelm)
{
List<XElement> newType = new List<XElement>();
List<XElement> typeOne = new List<XElement>(xelm.Elements("type One"));
List<XElement> typeTwo = new List<XElement>(xelm.Elements("type Two"));
typeTwo.Sort((c, d) => d.Attribute("name").Value.CompareTo(c.Attribute("name").Value));
typeOne.Sort((c, d) => d.Attribute("name").Value.CompareTo(c.Attribute("name").Value));
typeOne.ForEach(c => newType.Add(c.xelmSort()));
XElement outElm = new XElement(xelm.Name, xelm.Attribute("name"), xelm.Element("info"));
newType.ForEach(outElm.Add);
typeTwo.ForEach(outElm.Add);
return outElm;
}
After I was finished I needed to convert it back to an old style xml node so i used the following. This function will return a XmlDocument so you need to get the first child on it to get the node you started out with. Hope this helps.
public static XmlNode GetXmlNode(this XElement element)
{
using (XmlReader xmlReader = element.CreateReader())
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlReader);
return xmlDoc;
}
}