0

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(); 
          }
        }
3
  • What code have you tried so far, hopefully you are not expecting people to write this all for you. Commented Jul 25, 2012 at 8:56
  • Hi there, and welcome to your second week on SO! What have you tried yourself? Is there any specific piece of code you're having trouble with? Commented Jul 25, 2012 at 8:57
  • @ChrisBint : look in my CS File Code. Commented Jul 25, 2012 at 9:42

3 Answers 3

3

First Method: store the xml in a data set and then assign it to dropdownlist

DataSet ds=new DataSet();
ds.ReadXml("xmlfile.xml");

dropWerk.DataSource = ds; or dropWerk.DataSource = ds.Tables[0]; 
dropWerk.TextField = "field name"; // field to display in dropdown
dropWerk.ValueField="Value Field";
dropWerk.DataBind();

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

XmlDocument xdoc=new XmlDocument();
xdoc.Load("xmlfile.xml");

XmlNodeList node = xdoc.SelectSingleNodes("/NewDataSet/resources/");
foreach(XmlNode n in node )
{
ListItem l = new ListItem();
    l.Text = n.InnerXml.ToString();
    drpWerk.Items.Add(l);
}
drpWerk.DataBind();
Sign up to request clarification or add additional context in comments.

12 Comments

@Tarasov now what is your problem ?
I want create a new ListItem with this Value of <Werk>. If I start my Program I get a empty DropDownList :(
@Tarasov There is a mistake in your code: ds.ReadXml(XMLData.DataFile); dont specify xmldatasource in the readxml method just give the xmlfilename eg ds.ReadXml("xmlfile.xml"); or see my answer.
@Tarasov in which page event you are calling Bind Method ?
@Tarasov your code seems to right, place a breakpoint at ds.ReadXml(XMLData.DataFile); then right click and see the table contains the data or table is null
|
0

Try this:

<script runat="server">  
    protected void dropWerk_OnSelectedIndexChanged(object sender, System.EventArgs e) {  
        Label1.Text = dropWerk.SelectedItem.Text.ToString();  
}  
</script>  
<asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/App_Data/YourXmlFile.xml"></asp:XmlDataSource>
<asp:DropDownList ID="dropWerk" runat="server" Width="245px" DataSourceID="XmlDataSource1" OnSelectedIndexChanged="dropWerk_OnSelectedIndexChanged" />

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

1 Comment

I don't know what you mean with the Label1.Text = dropWerk.SelectedItem.Text.ToString(); because I create a new ListItem with the Value of the XML Value :/
0

The Solution:

private void BindXML()
        {
            XmlDocument xmldoc = new XmlDocument(); 

          xmldoc.Load(Server.MapPath("~/App_Data/Werke.xml"));

          XmlNodeList nodes = xmldoc.GetElementsByTagName("Werk");

            foreach (XmlNode n in nodes)
            {
                ListItem l = new ListItem();
                l.Text = n.InnerXml.ToString();
                dropWerk.Items.Add(l);
            }

            dropWerk.DataBind();

        }

bind this Method in the Page_Load Method and it works :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.