如何在asp.net中从XML数据源绑定DDL
本文关键字:XML 数据源 绑定 DDL 中从 net asp | 更新日期: 2023-09-27 17:57:51
我有一个xml文件,它是客户端给我的,所以我不能更改它,它看起来像这样:
CodeBase。XML
<?xml version="1.0" encoding="utf-8"?>
<CATALOG>
<orderByTitleDesc />
<orderByTitleDesc />
<orderByTitleDesc />
<orderByTitleDesc />
<orderByTitleDesc />
<CD>
<CodeBase>hellow</CodeBase>
</CD>
<CD>
<CodeBase>http://KL1225K7/EViewer/Client/EvCtrl6En.cab#version=9,4,2,9</CodeBase>
</CD>
<CD>
<CodeBase>31 amrch testing for font</CodeBase>
</CD>
<CD>
<CodeBase>CodeBase2</CodeBase>
</CD>
<CD>
<CodeBase>Ser</CodeBase>
</CD>
<CD>
<CodeBase>ZaidiTest</CodeBase>
</CD>
<CD>
<CodeBase>test222</CodeBase>
</CD>
</CATALOG>
我想在ddl 中将此xml与此绑定
我在代码隐藏文件中写代码如下:
protected void Page_Load(object sender, EventArgs e)
{
string filePath = Server.MapPath("CodeBase.xml");
using (DataSet ds = new DataSet())
{
ds.ReadXml(filePath);
ddlCodeBase.DataSource = ds.Tables["1"];
ddlCodeBase.DataTextField = "CodeBase";
ddlCodeBase.DataValueField = "CD";
ddlCodeBase.DataBind();
}
}
客户端代码如下:
<asp:DropDownList ID="ddlCodeBase" runat="server" ></asp:DropDownList>
但是我得到了空的DDl(里面没有数据)。请让我知道伙计们该怎么办。提前感谢。
请尝试以下代码。它对我来说运行良好。
string filePath = Server.MapPath("~/Cities.xml");
using (DataSet ds = new DataSet())
{
ds.ReadXml(filePath);
ddlCities.DataSource = ds;
ddlCities.DataTextField = "CodeBase";
ddlCities.DataValueField = "CodeBase";
ddlCities.DataBind();
}