I have a ASP.NET Application with a DropDownList and I want it fill with my XML File Values. How I can I use my XML for this that I create a new item for every Value in my XML.
My XML File:
<?xml version="1.0" standalone="yes" ?>
<NewDataSet>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="resources">
<xs:complexType>
<xs:sequence>
<xs:element name="Werk" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<resources>
<Werk>foo1</Werk> <!-- The first Value-->
</resources>
<resources>
<Werk>foo2</Werk> <!-- The second Value-->
</resources>
<resources>
<Werk>foo3</Werk> <!-- The third Value-->
</resources>
</NewDataSet>
In my ASPX:
<asp:XmlDataSource ID="XMLData" runat="server" DataFile="~/App_Data/Werke.xml" />
<asp:DropDownList ID="dropWerk" runat="server" Width="245px" />
I need a kind of a Method that fill my DropDownList with the Data from the XML File
tarasov
CS File:
private void BindXML()
{
XmlDocument xmldoc = XMLData.GetXmlDocument();
using (DataSet ds = new DataSet())
{
ds.ReadXml(XMLData.DataFile);
dropWerk.DataSource = ds;
dropWerk.DataTextField = "Werk";
dropWerk.DataBind();
}
}
Try this:
You may want to put a breakpoint in dropWerk_OnSelectedIndexChanged handler and examine dropWerk.SelectedItem to get ideas how to better use the passed value
The Solution:
bind this Method in the Page_Load Method and it works :)
First Method: store the xml in a data set and then assign it to dropdownlist
Updated Answer: *2nd Method:* add one by one value to your dropdownlist items If I understand your question from the comments you want to add xml values to list items try this