0

I have an XML document for which I want to sort specific nodes alphabetically.

XML document

<response>
    <lst name="facet_counts">
        <lst name="facet_fields">
            <lst name="professions_raw_nl">
                <int name="Pharmacy">2724</int>
                <int name="Physiotherapy">2474</int>
                <int name="Doctor">2246</int>
                <int name="Dentist">1309</int>
            </lst>  
        </lst>
    </lst>
</response> 

Desired output
Dentist (1309)
Doctor (2246)
Pharmacy (2724)
Physiotherapy (2474)

Current ASP.NET code

dim node as XmlNode = objXML.SelectSingleNode("response/lst[@name=""facet_counts""]/lst[@name=""facet_fields""]/lst[@name=""professions_raw_nl""]")
Dim sbuilder As New StringBuilder
Dim navigator As XPathNavigator = node.CreateNavigator()
Dim selectExpression As XPathExpression = navigator.Compile("???") <-- what expression should I use here ???
selectExpression.AddSort("????", XmlSortOrder.Ascending, XmlCaseOrder.None, "", XmlDataType.Text) <-- what expression should I use here ????
Dim nodeIterator As XPathNodeIterator = navigator.Select(selectExpression)
While nodeIterator.MoveNext()
    'how can I print the name and value of the node?
End While
John Saunders
  • 160,644
  • 26
  • 247
  • 397
Adam
  • 6,041
  • 36
  • 120
  • 208

3 Answers3

0

if you are using .Net>=3.5 then you can use linq to do this for you like this

var xmlString = "YOUR XML STRING";
var doc = XDocument.Parse(xmlString);

var list = doc. Descendants("int").select(n=>new {Name = n.Attribute("name").Value, Value = n.Value});
var sortedList = list.OrderBy(l=>l.Name);

EDIT not a pro in VB but try this - pls there may be syntactical errors in here

dim xmlString as string
xmlString = "YOUR XML STRING"
dim doc as XDocument()
doc = XDocument.Parse(xmlString)
dim list = doc.Descendants("int").select(n=>new {Name = n.Attribute("name").Value, Value = n.Value}).OrderBy(l=>l.Name)

LINQ stands for Language Integrated Query.. and is a lot easier and Uniform that the thing you are using currently.. you can read more here

Parv Sharma
  • 12,581
  • 4
  • 48
  • 80
  • Now we're entering a side discussion. I use VB.NET, how can I use your code in there? Also, why would I want to use Linq instead of just VB.NET? – Adam Oct 17 '12 at 21:35
  • Doing this kind of processing in a high-level language designed for XML manipulation (such as XSLT or Linq) is always going to be much easier than writing it with low-level procedural code - once you've learned to use the tools, of course. – Michael Kay Oct 18 '12 at 08:43
0

I tried the following code and it is working (C#) you can get values as below

        var doc = new XmlDocument();
        doc.Load("c:\\users\\ozgur\\sample.xml");
        var nav = doc.CreateNavigator();
        var node = nav.SelectSingleNode("response").SelectSingleNode("lst[@name=\"facet_counts\"]").SelectSingleNode("lst[@name=\"facet_fields\"]").SelectSingleNode("lst[@name=\"professions_raw_nl\"]").Select("int");

        var sorted = new SortedDictionary<string, string>();
        while (node.MoveNext())
        {
            var name = node.Current.SelectSingleNode("@name").Value;
            var value = node.Current.Value;
            sorted.Add(name, value);
        }

        foreach (var item in sorted)
        {
            item.Key.ToString();
            item.Value.ToString();
        }
Ozgur
  • 258
  • 3
  • 13
  • Where in your code is the actual sorting taking place? Could you use my own code as the base for your answer? – Adam Oct 17 '12 at 21:43
  • in while you can user sorted list, it will be just two lines of code – Ozgur Oct 17 '12 at 21:49
  • I'm not sure what you're saying, but my question was how I could sort the nodes. But do I understand correctly that you did not provide this code? – Adam Oct 17 '12 at 21:52
  • First you need to get the node values right, I updated the code sample – Ozgur Oct 17 '12 at 21:57
0

Since all answers seem to work with Linq and I just wanted to use regular VB.NET I now added each node in an Arraylist and used the regular Array.Sort() on it. Does the trick.

Adam
  • 6,041
  • 36
  • 120
  • 208