Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

sort menu items from xml

Posted on 2011-09-19
4
Medium Priority
?
310 Views
Last Modified: 2012-06-27
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);
            da.Fill(ds);
            da.Dispose();
        }

        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;
        ds.Relations.Add(relation);

        xmlDataSource.Data = ds.GetXml();
    }

Open in new window


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

0
Comment
Question by:JCWEBHOST
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 16

Accepted Solution

by:
Easwaran Paramasivam earned 500 total points
ID: 36558694
In the database store the sub menu items in the same order as you wish to sort either ascending or descending.

0
 
LVL 8

Assisted Solution

by:gena17
gena17 earned 500 total points
ID: 36558901
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.
0
 
LVL 3

Assisted Solution

by:ExpertLogin_890
ExpertLogin_890 earned 500 total points
ID: 36559037
for softing the xml data go through with below links

http://www.example-code.com/vbdotnet/xmlSort.asp

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);
            exp.AddSort("text()",XmlSortOrder.Ascending,XmlCaseOrder.None,"",
                XmlDataType.Text);

            //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.
            //
            InitializeComponent();
            base.OnInit(e);
        }
       
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {    

        }
        #endregion
    }
}
0
 
LVL 7

Assisted Solution

by:gopaltayde
gopaltayde earned 500 total points
ID: 36559991
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.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question