Search This Blog

Saturday, October 25, 2008

How to sort XML without XSL in the .NET Framework

i found a very nice article that describe how to sort XML without XSL in the .NET Framework , with a sample code , you can find the complete article here .

basically i use it in this way too:




XmlDocument doc = new XmlDocument();
doc.LoadXml(mergeXML);
XPathNavigator navigator = doc.CreateNavigator();
XPathExpression expression = navigator.Compile("Messages/Message/Date");
expression.AddSort("@tick", XmlSortOrder.Descending, XmlCaseOrder.UpperFirst, string.Empty, XmlDataType.Number);
XPathNodeIterator iterator = navigator.Select(expression);
StringBuilder sb = new StringBuilder();
foreach (XPathNavigator item in iterator)
{
// we have here the Date node we want the Message node
sb.Append(((XmlNode)item.UnderlyingObject).ParentNode.OuterXml);
}

No comments: