How I can fill my DropDownList with Data from a XM

2019-09-19 02:45发布

问题:

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(); 
          }
        }

回答1:

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();


回答2:

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



回答3:

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 :)