• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 313
  • Last Modified:

sort menu items from xml

Hey guys i have an menu and populating it from a data base

here the code in c#:

protected void Page_Load(object sender, EventArgs e)
        DataSet ds = new DataSet();
        using (SqlConnection conn = new SqlConnection(connStr))
            string sql = "Select id, title, parent_id, url from menu";
            SqlDataAdapter da = new SqlDataAdapter(sql, conn);

        ds.DataSetName = "Menus";
        ds.Tables[0].TableName = "menu";
        DataRelation relation = new DataRelation("ParentChild", ds.Tables["menu"].Columns["id"], ds.Tables["menu"].Columns["parent_id"], true);

        relation.Nested = true;

        xmlDataSource.Data = ds.GetXml();

Open in new window

my problem is, how do to do a sort on the sub menus?

4 Solutions
Easwaran ParamasivamCommented:
In the database store the sub menu items in the same order as you wish to sort either ascending or descending.

If you do want to sort the sub-menus, but do not want to sort the root level items, I suggest to make a copy of the table withing the DataSet after you bring the data from the database. Then sort the second table and create a relation between the two tables.
for softing the xml data go through with below links


other wise you can try below code

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Xml.XPath;
using System.Text;

namespace CodeBank.System_Xml_XPath {
    /// <summary>
    /// Summary description for SortingXML.
    /// </summary>
    public partial class SortingXML : System.Web.UI.Page {
        protected void Page_Load(object sender, System.EventArgs e) {
            StringBuilder unsorted = new StringBuilder();
            StringBuilder sorted = new StringBuilder();
            string xpath = "/Customers/Customer/ContactName";

            //Create XPathDocument class so we can get a navigator
            XPathDocument doc = new XPathDocument(Server.MapPath("customers.xml"));
            XPathNavigator nav = doc.CreateNavigator();

            XPathNodeIterator nodeIter1 = nav.Select(xpath);
            while (nodeIter1.MoveNext()) {
                unsorted.Append(nodeIter1.Current.Value + "<br />");
            this.lblUnsorted.Text = unsorted.ToString();

            //Compile xpath expression so we can add a sort to it
            XPathExpression exp = nav.Compile(xpath);

            //select nodes so we can see the sort
            XPathNodeIterator nodeIter2 = nav.Select(exp);
            while (nodeIter2.MoveNext()) {
                sorted.Append(nodeIter2.Current.Value + "<br />");
            this.lblSorted.Text = sorted.ToString();

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()

populate below two tables into dataset
1) "Select id, title, parent_id, url from menu where parent_id is null"
2) "Select id, title, parent_id, url from menu where parent_id is not null order by id" (or order by title)

Then set the relation between them.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now