ASPNET Fill dropdownlist with XML

I have an eg. of an XML file:

<data>
<dropdownlist1>
    <id>1</id>
    <value>example1</value>
</dropdownlist1>

<dropdownlist1>
    <id>2</id>
    <value>example2</value>
</dropdownlist1>

<dropdownlist2>
    <id>1</id>
    <value>example1</value>
</dropdownlist2>

<dropdownlist2>
    <id>2</id>
    <value>example2</value>
</dropdownlist2>

I dont know if im doing the XML file right, what I want is pick the values of the tag dropdownlist1 to put in a dropdownlist, and the values of the dropdownlist2 to put in another dropdownlist. Somebody please can help me? Thanks, and sorry my english.

Upvotes: 0

Views: 44

Answers (1)

Kris Krause
Kris Krause

Reputation: 7326

One way is to use the XmlDataSource control and bind to your dropdown.

https://msdn.microsoft.com/en-us/library/494y92bs%28v=vs.140%29.aspx

Note: Your Xml is missing the ending "data" tag.

You can utilize the XPath property of your XmlDataSource to filter your Xml. It might look like -

      <asp:XmlDataSource
          id="MyDataSource"
          runat="server"
          XPath="/dropdownlist1"
          DataFile="~/App_Data/myExample.xml" />

https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.xmldatasource.xpath(v=vs.100).aspx

Upvotes: 1

Related Questions