讀取XML并綁定至RadioButtonList實(shí)現(xiàn)思路及演示動(dòng)畫
更新時(shí)間:2013年01月23日 09:48:55 作者:
讀取XML的文檔,可以使用System.Data.DataSet類別中的ReadXml()方法,在aspx網(wǎng)頁(yè)上拉一個(gè)RadioButtonList控件,用來(lái)顯示XML的數(shù)據(jù),接下來(lái),用DataSet去讀取剛才寫好的獲取XML文件的屬性,即可完成
讀取XML的文檔,可以使用System.Data.DataSet類別中的ReadXml()方法。如下面的xml文檔,放在站點(diǎn)的根目錄之下:
YearOfBirth.xml
<?xml version="1.0" encoding="utf-8" ?>
<YearOfBirths>
<YearOfBirth>
<ID>1</ID>
<Name>鼠</Name>
</YearOfBirth>
<YearOfBirth>
<ID>2</ID>
<Name>牛</Name>
</YearOfBirth>
<YearOfBirth>
<ID>3</ID>
<Name>虎</Name>
</YearOfBirth>
<YearOfBirth>
<ID>4</ID>
<Name>兔</Name>
</YearOfBirth>
<YearOfBirth>
<ID>5</ID>
<Name>龍</Name>
</YearOfBirth>
<YearOfBirth>
<ID>6</ID>
<Name>蛇</Name>
</YearOfBirth>
<YearOfBirth>
<ID>7</ID>
<Name>馬</Name>
</YearOfBirth>
<YearOfBirth>
<ID>8</ID>
<Name>羊</Name>
</YearOfBirth>
<YearOfBirth>
<ID>9</ID>
<Name>猴</Name>
</YearOfBirth>
<YearOfBirth>
<ID>10</ID>
<Name>雞</Name>
</YearOfBirth>
<YearOfBirth>
<ID>11</ID>
<Name>狗</Name>
</YearOfBirth>
<YearOfBirth>
<ID>12</ID>
<Name>豬</Name>
</YearOfBirth>
</YearOfBirths>
使用一個(gè)屬性來(lái)獲取這個(gè)文檔:
private string XmlFile
{
get
{
return Server.MapPath("~/YearOfBirth.xml");
}
}
在aspx網(wǎng)頁(yè)上拉一個(gè)RadioButtonList控件,用來(lái)顯示XML的數(shù)據(jù)。
<asp:RadioButtonList ID="RadioButtonListYearOfBirth" runat="server" RepeatColumns="6" RepeatDirection="Horizontal"></asp:RadioButtonList>
接下來(lái),用DataSet去讀取剛才寫好的獲取XML文件的屬性。
View Code
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
Data_Binding();
}
private void Data_Binding()
{
using (DataSet ds = new DataSet())
{
ds.ReadXml(XmlFile);
this.RadioButtonListYearOfBirth.DataSource = ds;
this.RadioButtonListYearOfBirth.DataTextField = "Name";
this.RadioButtonListYearOfBirth.DataValueField = "ID";
this.RadioButtonListYearOfBirth.DataBind();
}
}
}
網(wǎng)頁(yè)運(yùn)行效果:
YearOfBirth.xml
復(fù)制代碼 代碼如下:
<?xml version="1.0" encoding="utf-8" ?>
<YearOfBirths>
<YearOfBirth>
<ID>1</ID>
<Name>鼠</Name>
</YearOfBirth>
<YearOfBirth>
<ID>2</ID>
<Name>牛</Name>
</YearOfBirth>
<YearOfBirth>
<ID>3</ID>
<Name>虎</Name>
</YearOfBirth>
<YearOfBirth>
<ID>4</ID>
<Name>兔</Name>
</YearOfBirth>
<YearOfBirth>
<ID>5</ID>
<Name>龍</Name>
</YearOfBirth>
<YearOfBirth>
<ID>6</ID>
<Name>蛇</Name>
</YearOfBirth>
<YearOfBirth>
<ID>7</ID>
<Name>馬</Name>
</YearOfBirth>
<YearOfBirth>
<ID>8</ID>
<Name>羊</Name>
</YearOfBirth>
<YearOfBirth>
<ID>9</ID>
<Name>猴</Name>
</YearOfBirth>
<YearOfBirth>
<ID>10</ID>
<Name>雞</Name>
</YearOfBirth>
<YearOfBirth>
<ID>11</ID>
<Name>狗</Name>
</YearOfBirth>
<YearOfBirth>
<ID>12</ID>
<Name>豬</Name>
</YearOfBirth>
</YearOfBirths>
使用一個(gè)屬性來(lái)獲取這個(gè)文檔:
復(fù)制代碼 代碼如下:
private string XmlFile
{
get
{
return Server.MapPath("~/YearOfBirth.xml");
}
}
在aspx網(wǎng)頁(yè)上拉一個(gè)RadioButtonList控件,用來(lái)顯示XML的數(shù)據(jù)。
復(fù)制代碼 代碼如下:
<asp:RadioButtonList ID="RadioButtonListYearOfBirth" runat="server" RepeatColumns="6" RepeatDirection="Horizontal"></asp:RadioButtonList>
接下來(lái),用DataSet去讀取剛才寫好的獲取XML文件的屬性。
復(fù)制代碼 代碼如下:
View Code
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
Data_Binding();
}
private void Data_Binding()
{
using (DataSet ds = new DataSet())
{
ds.ReadXml(XmlFile);
this.RadioButtonListYearOfBirth.DataSource = ds;
this.RadioButtonListYearOfBirth.DataTextField = "Name";
this.RadioButtonListYearOfBirth.DataValueField = "ID";
this.RadioButtonListYearOfBirth.DataBind();
}
}
}
網(wǎng)頁(yè)運(yùn)行效果:
相關(guān)文章
MVC使用極驗(yàn)驗(yàn)證制作登錄驗(yàn)證碼學(xué)習(xí)筆記7
這篇文章主要介紹了MVC使用極驗(yàn)驗(yàn)證制作登錄驗(yàn)證碼學(xué)習(xí)筆記,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
ASP.NET MVC使用RazorEngine解析模板生成靜態(tài)頁(yè)
這篇文章主要介紹了ASP.NET MVC使用RazorEngine解析模板生成靜態(tài)頁(yè)的相關(guān)資料,需要的朋友可以參考下2016-05-05
在ASP.Net?Core應(yīng)用程序中使用Bootstrap4
這篇文章介紹了在ASP.Net?Core應(yīng)用程序中使用Bootstrap4的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-01-01
在.NET Core控制臺(tái)程序中如何使用依賴注入詳解
這篇文章主要給大家介紹了關(guān)于在.NET Core控制臺(tái)程序中如何使用依賴注入的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-10-10
在 asp.net core 的中間件中返回具體的頁(yè)面的實(shí)現(xiàn)方法
這篇文章主要介紹了在 asp.net core 的中間件中返回具體的頁(yè)面的實(shí)現(xiàn)方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08

