讀取XML并綁定至RadioButtonList實現(xiàn)思路及演示動畫
更新時間:2013年01月23日 09:48:55 作者:
讀取XML的文檔,可以使用System.Data.DataSet類別中的ReadXml()方法,在aspx網(wǎng)頁上拉一個RadioButtonList控件,用來顯示XML的數(shù)據(jù),接下來,用DataSet去讀取剛才寫好的獲取XML文件的屬性,即可完成
讀取XML的文檔,可以使用System.Data.DataSet類別中的ReadXml()方法。如下面的xml文檔,放在站點的根目錄之下:
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>
使用一個屬性來獲取這個文檔:
private string XmlFile
{
get
{
return Server.MapPath("~/YearOfBirth.xml");
}
}
在aspx網(wǎng)頁上拉一個RadioButtonList控件,用來顯示XML的數(shù)據(jù)。
<asp:RadioButtonList ID="RadioButtonListYearOfBirth" runat="server" RepeatColumns="6" RepeatDirection="Horizontal"></asp:RadioButtonList>
接下來,用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)頁運行效果:
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>
使用一個屬性來獲取這個文檔:
復制代碼 代碼如下:
private string XmlFile
{
get
{
return Server.MapPath("~/YearOfBirth.xml");
}
}
在aspx網(wǎng)頁上拉一個RadioButtonList控件,用來顯示XML的數(shù)據(jù)。
復制代碼 代碼如下:
<asp:RadioButtonList ID="RadioButtonListYearOfBirth" runat="server" RepeatColumns="6" RepeatDirection="Horizontal"></asp:RadioButtonList>
接下來,用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)頁運行效果:
相關(guān)文章
ASP.NET MVC使用RazorEngine解析模板生成靜態(tài)頁
這篇文章主要介紹了ASP.NET MVC使用RazorEngine解析模板生成靜態(tài)頁的相關(guān)資料,需要的朋友可以參考下2016-05-05
在ASP.Net?Core應(yīng)用程序中使用Bootstrap4
這篇文章介紹了在ASP.Net?Core應(yīng)用程序中使用Bootstrap4的方法,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-01-01
在 asp.net core 的中間件中返回具體的頁面的實現(xiàn)方法
這篇文章主要介紹了在 asp.net core 的中間件中返回具體的頁面的實現(xiàn)方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08

