Kirk Allen Evans talks about sorting XML with System.Xml.XPath.XPathExpression
[via Kirk Allen Evan’s Blog]
<snip>
This question came up twice this week, so I thought it warranted a quick post. If you want to retrieve a sorted list of nodes from an XmlDocument instance, there are a couple ways to do this. You can use XSLT, or you can write your own sorting algorithm. The .NET Framework provides capability to provide sort orders to the result of an xpath query through the System.Xml.XPath.XPathExpression::AddSort method. Here is a simplistic demo:
Dan Wahlin maintains an excellent resource site, XML For ASP.NET, to find quick HowTo's on various XML technologies.20 static void Main( string[] args )
21 {
22
23 XmlDocument doc = new XmlDocument();
24 doc.LoadXml( "<a><b>5</b><b>3</b><b>6</b><b>1</b></a>" );
25 XPathNavigator nav = doc.CreateNavigator();
26 string path = "/a/b";
27 XPathExpression expression = nav.Compile( path );
28 expression.AddSort( "text()", XmlSortOrder.Ascending, XmlCaseOrder.None, string.Empty, XmlDataType.Number );
29 XPathNodeIterator iterator = nav.Select( expression );
30 while( iterator.MoveNext() )
31 {
32 Console.WriteLine( iterator.Current.Value );
33 }
34 }
</snip>
Recent Posts
- Understanding API First Strategy and Benefits
- Frankenstein APIs Explained! - API Cyber Security Series
- API Security 101 - Cyber Security Explained
- API Trends 2022 - API Security and Cybersecurity
- API Trends 2022 - Seamless Integration Solutions
- API Trends 2022 - Adaptive API Management
- API Trends 2022 - API Integration Automation
- API Trends 2022 - Industry Specific Breakouts
- API Trends 2022 - API Best Practices
- API Trends 2022 - Open API Standards
- API Trends 2022 - API Integration Experience
- API Trends 2022 - API-Led Modernization
- API Trends 2022 - API Economy Growth
- Brenton House - Give your App and APIs a Turbo Boost – Part 2
- Easily Enable Speech Recognition in Titanium iOS using Hyperloop
Tag Cloud
- .NET
- adaptive api management
- android
- api
- api automation
- api best practices
- api economy
- api experience
- api first
- api integration
- api integrations
- api management
- api security
- api standards
- api strategy
- api trends
- apis
- appcelerator
- automation
- boot
- Brenton House
- C#
- Cisco VPN
- Cloud
- Community News
- Continuous Integration
- crash
- cybersecurity
- Dropbox
- fhir apis
- Free Stuff
- General Software Development
- Google Drive
- graphql
- healthcare apis
- houserules
- hyperloop
- ios
- json schema
- Live Mesh
- Mac OS X
- Microsoft
- mobile
- mobile api
- native
- open banking
- openapi
- Other Stuff
- SkyDrive
- speech
- Stuff
- swagger
- Team Systems
- titanium native
- titanium turbo
- Unit Testing
- Vista
- Visual Studio
- Visual Studio 11
- voice
- win7
- Windows 7
- Windows 8
- windows update