如何在我的ASP.NET应用程序中用XML文件的数据填充我的DropDownList?[英] How I can fill my DropDownList with Data from a XML File in my ASP.NET Application

本文是小编为大家收集整理的关于如何在我的ASP.NET应用程序中用XML文件的数据填充我的DropDownList?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

本文来自:IT宝库(https://www.itbaoku.cn)

我有一个带有下拉列表的ASP.NET应用程序,我希望它填充我的XML文件值. 我如何将XML用于此我为XML中的每个值创建一个新项目.

我的XML文件:

<?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>

在我的aspx中:

<asp:XmlDataSource ID="XMLData" runat="server" DataFile="~/App_Data/Werke.xml" />
        <asp:DropDownList ID="dropWerk" runat="server" Width="245px" />

我需要一种方法,将我的下拉列表填充来自XML文件的数据

塔拉索夫

CS文件:

private void BindXML()
        {
          XmlDocument xmldoc = XMLData.GetXmlDocument();

          using (DataSet ds = new DataSet())
          {
              ds.ReadXml(XMLData.DataFile);
              dropWerk.DataSource = ds;
              dropWerk.DataTextField = "Werk";
              dropWerk.DataBind(); 
          }
        }

推荐答案

第一个方法: 将XML存储在数据集中,然后将其分配给下拉列表

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

更新答案: * 第二种方法:*一个一个值添加到您的下拉列表项目 如果我从评论中理解您的问题,您要添加XML值以列出项目 尝试这个

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

其他推荐答案

尝试以下方法:

<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" />

您可能想在dropwerk_onselectedIndexchanged处理程序中放置一个断点,然后检查dropwerk.selectedItem以获取想法如何更好地使用传递的值

其他推荐答案

解决方案:

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

        }

在page_load方法中绑定此方法,它有效:)

本文地址:https://www.itbaoku.cn/post/269942.html

问题描述

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

推荐答案

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

其他推荐答案

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

其他推荐答案

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