Bind XML to Drodownlist

madmare
madmare used Ask the Experts™
on
HI,
 I have an XML File Like this
<Category>
  <note Title="p501">
    <param1>Tove</param1>
    <param2>Jani</param2>
    <param3>Reminder</param3>
  </note>

  <note Title="p502">
    <param1>Jani</param1>
    <param2>Tove</param2>
    <param3>Re: Reminder</param3>
  </note>
</Category>

How Do I Bind The "Title" in the XML file at a dropdown list.
Also I need a good way to get the params of a selected title in the drodownlist

I am using ASP.NET 2005 (C#)
Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
This should do the trick for you..
http://www.w3schools.com/aspnet/aspnet_xml.asp

it should also work for a dropdownlist.....

as for the second part,you can write it in the dropdownlist selected index change event...

XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("Menu.xml"));
XmlNodeList nodeList = doc.SelectNodes(dropdownlist.selectednodevalue);
foreach(XmlNode node in nodeList)
 DropDownList.Items.Add(new ListItem(node.SelectSingleNode("param1").InnerText));
Dim doc as New XmlDOcument();
doc.Load(Server.MapPath("yourfile.xml"));

DropDownList1.DataSource = doc.SelectNodes("/Category/note[@Title]");
DropDownList1.DataBind();

Author

Commented:
C# please

Commented:
apart from the first line all the other lines are in C#...

XmlDocument doc = new XmlDocument();
sorry about that - should be

XmlDOcument doc=New XmlDocument();
doc.Load(Server.MapPath("yourfile.xml"));

DropDownList1.DataSource = doc.SelectNodes("/Category/note[@Title]");
DropDownList1.DataBind();



Author

Commented:
my question was how do I get the value of param1 for the selected "Title" at the Dropdownlist
doc.SelectSingleNode("/Category/note[@Title='" + DropDownList1.SelectedValue + "']");
XmlNode xnode=doc.SelectSingleNode("/Category/note[@Title='" + DropDownList1.SelectedValue + "']/param1");

String param1=xnode.InnerText
oops, forgot the semicolon after InnerText

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial