Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
| Search | Today's Posts | Mark Forums Read
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 27th, 2007, 05:36 AM
Authorized User
 
Join Date: Nov 2007
Location: , , Ireland.
Posts: 67
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Hughesie78
Default Sorting XML data in a dropdown list on asp.net

Hi there

I have a drop down list reading from an XML Doc into a dataset and an XMLDataDOc, it is pulling in a list of unqique values from the XML doc and loading them to a drop down list, my prolem is that they are not sorted. I want to sort them alplabetically but im not sure who to do this, I read a suggestion yesterday to use an XSL file, but I am not sure how to incorporate this into my code below, any suggestions..
        if (!Page.IsPostBack)
        // load t
        {
            DataSet ds = new DataSet();
            ds.ReadXml(Server.MapPath("Banks.xml"));
            XmlDataDocument XMLDoc = new XmlDataDocument(ds);
            foreach (XmlNode XMLNodes in XMLDoc.SelectNodes("//bank[not(.=preceding::bank)]"))
            {

                ddl_Banks.Items.Add(new ListItem(XMLNodes.ChildNodes[0].InnerText.ToString()));

            }
        }

__________________
Thank You
 
Old November 27th, 2007, 06:00 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

I'd recommend creating a SortedList, adding the XML contents to that, and using that as the data source for the drop down list.

Plus, you don't need to do InnerText.ToString(), InnerText is already a string.

/- Sam Judson : Wrox Technical Editor -/
 
Old November 27th, 2007, 10:10 AM
Authorized User
 
Join Date: Nov 2007
Location: , , Ireland.
Posts: 67
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Hughesie78
Default

hi

thanks for the Tostring() tip, i changed it and it works fine cheers.
Would you have an example of how i can assign my XML to a sorted list, ive been looking at sorted lists and i know how to add elements throught code using the Add() method, but how can I add my XML file above and then create the Dropdownlist from it?
 
Old November 27th, 2007, 10:19 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

SortedList list = new SortedList();

foreach (XmlNode node in XMLDoc.SelectNodes("//bank[not(.=preceding::bank)]"))
{
   list.Add(node.InnerText);
}

ddl_Banks.DataSource = list;
ddl_Banks.DataBind();


/- Sam Judson : Wrox Technical Editor -/
 
Old November 27th, 2007, 11:50 AM
Authorized User
 
Join Date: Nov 2007
Location: , , Ireland.
Posts: 67
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Hughesie78
Default

thank you this work brilliant.




Similar Threads
Thread Thread Starter Forum Replies Last Post
Editable Dropdown list in ASP.NET Anypond BOOK: Wrox's ASP.NET 2.0 Visual Web Developer 2005 Express Edition Starter ISBN: 978-0-7645-8807-5 5 February 17th, 2011 09:43 PM
Sorting XML in a dropdown list Hughesie78 ADO.NET 1 November 23rd, 2007 08:09 AM
Data Access Page dropdown list blank? kalebson Access 1 October 4th, 2006 07:44 AM
Data from Dropdown List to TextBox caterpillar General .NET 1 July 10th, 2006 06:05 AM
Issue with dropdown list and submit - ASP.NET Montenegro ASP.NET 1.0 and 1.1 Basics 0 March 24th, 2004 04:40 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.