Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XPath -> get unique nodes values from XML doc

Status
Not open for further replies.

AT76

Technical User
Apr 14, 2005
460
US
Hi all,

I'm working on an ASP app in which I need to load an XML file and the display a dropdownlist which when clicked it shows the unique values for a specific node:

Ex.

Code:
<?xml version="1.0"?>
<!-- A fragment of a book store inventory database -->
<bookstore xmlns:bk="urn:samples">
  <book genre="novel" publicationdate="1997" bk:ISBN="1-861001-57-8">
    <title>Pride And Prejudice</title>
    <author>
      <first-name>Jane</first-name>
      <last-name>Austen</last-name>
    </author>
    [b][COLOR=red]<type>fiction</type>[/color][/b]
  </book>
  <book genre="novel" publicationdate="1992" bk:ISBN="1-861002-30-1">
    <title>The Handmaid's Tale</title>
    <author>
      <first-name>Margaret</first-name>
      <last-name>Atwood</last-name>
    </author>
    [b][COLOR=red]<type>fiction</type>[/color][/b]
  </book>
  <book genre="novel" publicationdate="1991" bk:ISBN="1-861001-57-6">
    <title>Emma</title>
    <author>
      <first-name>Jane</first-name>
      <last-name>Austen</last-name>
    </author>
    [b][COLOR=red]<type>romance</type>[/color][/b]
  </book>
  <book genre="novel" publicationdate="1982" bk:ISBN="1-861001-45-3">
    <title>Sense and Sensibility</title>
    <author>
      <first-name>Jane</first-name>
      <last-name>Austen</last-name>
    </author>
    [b][COLOR=red]<type>romance</type>[/color][/b]
  </book>
</bookstore>


I would like my dropdown list to display :

fiction
romance

I'm currently able to display:

fiction
fiction
romance
romance

Here's the code I have:

Code:
XmlDocument xDoc = new XmlDocument();
            xDoc.Load(xmlPath);
            [b]XmlNodeList audienceList = xDoc.SelectNodes("/bookstore/book/type");[/b]


            foreach (XmlNode n in audienceList)
            {
                ListItem item = new ListItem(n.InnerText, n.InnerText);
                this.selection.Items.Add(item);
            }


Could someone point me in the write direction as to how to get the unique values for type?

Thank you!
 
The filter you referenced doesn't let you order the results from what i can tell. Another way to accomplish this would be to load the xml into a datatable and do a select on it with an order by clause. This would give you the results you wanted in order. Then as you loop through the results, have your c# check the previous before adding it to the listitem to see if it is a repeat. This solution may not be as elegant but it does accomplish the task. I would be interested to know which way is most economical in performance...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top