I have an XML here as follows,

<?xml version="1.0" encoding="ISO-8859-1"?>
<countries>
<country>
  <text>Norway</text>
</country>
<country>
  <text>Sweden</text>
</country>
<country>
  <text>France</text>
</country>
<country>
  <text>Italy</text>
</country>
</countries>

And, I am trying to sort it without using XSLT, and I am not using this to edit the XML file. I only would like to display it.

Here is what I come up with:

countryList = document.SelectNodes("/countries/country")              
    Dim country As XmlNode   
    Dim count_record As Integer = country.Count-1  
      For i As Integer = 0 to count_record  
            country = countryList.Item(i)  
            Dim country_name As XmlNode = country.FirstChild  
            countryNodesOut.Text &= "<b>Name:</b> " & country_name.InnerText & "<br/>"  
         Next

Based on this, what do I need to do so that it would be "sorted"?

Thanks for your help.

Take a look at LINQ - XML.

var result = from v in XDocument.Load(@"c:\folder\sample.xml").Descendants("country")
                          orderby v.Value  
                         select v;

  foreach (var t in result)
        Console.WriteLine(t);

Take a look at LINQ - XML.

var result = from v in XDocument.Load(@"c:\folder\sample.xml").Descendants("country")
                          orderby v.Value  
                         select v;

  foreach (var t in result)
        Console.WriteLine(t);

I was just about to post this before I received your reply.
The xml:

<?xml version="1.0" encoding="ISO-8859-1"?>
<countries>
<country>
  <text>Norway</text>
</country>
<country>
  <text>Sweden</text>
</country>
<country>
  <text>France</text>
</country>
<country>
  <text>Italy</text>
</country>
</countries>

This is what I have come up with, but I got Compiler Error Message: BC30451: Name 'XDocument' is not declared.

Have I missed something here?

<script runat="server">
Sub Page_Load

    Dim doc = XDocument.Load(dataPath + "countries.xml")
    Dim query = From song In doc...<song> _
                Order By CStr(country...<text>.First())_
                Select country
    For Each result In query
    myNodesOut.text &= result
    Next
End Sub
</script>

<html>
<body>
<form runat="server">
<asp:label id="myNodesOut" runat="server" />
</form>
</body>
</html>

Thanks for your help.

adatapost,

I followed your example and modified my example to using your lines, but it looks like you are writing in C#. Am I wrong?

Here is my updated version:

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.XML" %>

<script runat="server">
Sub Page_Load


 Dim file As String = Context.Server.MapPath("countries.xml")
 Dim document As XmlDocument = New XmlDocument()
 document.Load(file)

 Dim query = From country In doc...<country> _  
             Order By CStr(country...<text>.First())_
             Select country

 End Select
 
 For Each result In query
   myNodesOut.text &= result
 Next

End Sub
</script>

<html>
<body>
<form runat="server">
<asp:label id="myNodesOut" runat="server" />
</form>
</body>
</html>

And, the errors:

C:\inetpub\wwwroot\asp_test\sort.aspx(12) : error BC30205: End of statement expected.

Dim query = From country In doc...<country> _
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Order By CStr(country...<text>.First())_
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
C:\inetpub\wwwroot\asp_test\sort.aspx(14) : error BC30451: Name 'country' is not declared.

Select country
~~~~~~~
C:\inetpub\wwwroot\asp_test\sort.aspx(18) : error BC30451: Name 'result' is not declared.

For Each result In query
~~~~~~
C:\inetpub\wwwroot\asp_test\sort.aspx(18) : error BC30451: Name 'query' is not declared.

For Each result In query
~~~~~

Am I missing something here? I edited this from an example I found that uses LinQ.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.