Click here to Skip to main content
15,747,637 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am reading a xml file and trying to populate one of the nodes into a dropdownbox. I've managed to do this. However the dropdownbox shows every node innertext and I need to get rid of the duplicates.

XmlDocument doc = new XmlDocument();

            doc.Load(xml_Url);

            XmlNodeList nodes = doc.SelectNodes("JOBS/JOB/CATEGORIES/CATEGORY");
            

            foreach (XmlNode node in nodes)
            {

             DropDownList1.Items.Add(newListItem(node.FirstChild.InnerText, node.LastChild.InnerText));

            }
Posted
Updated 25-May-10 3:05am
v2

The following usis Xml.Linq (I prefer this to the original Xml classes in .Net 2.0). Caveat - I did this from memory, so you may have to tweak it a little).

XDocument doc = XDocument.Load(xm_Uri);
XElement root = doc.Element("JOBS/JOB/CATEGORIES");
foreach (XElement element in root.Elements())
{
    DropDownList1.Items.Add(element.Element("Category").Value.ToString());
}
 
Share this answer
 
Comments
Luke.Price 28-May-10 7:05am    
Thank you
watson128 23-Jun-10 8:47am    
Reason for my vote of 1
This does not answer the question, AT ALL.
You can use the ListBox (which is what your DropDownBox is I assume) method: DropDownList1.Items.Contains(entry) as follows:
  foreach (XmlNode node in nodes)
            {
      if(!DropDownList1.Items.Contains(node.FirstChild.InnerText)
            { DropDownList1.Items.Add(newListItem(node.FirstChild.InnerText, node.LastChild.InnerText));
}
            }


This will check to see if the new entry is already in the list and only add it if not.

Hope this helps.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900