读取XML并绑定至RadioButtonList实现思路及演示动画

西沉的红日,把缕缕落寂的橘红涂满天际。夕阳下,沧桑古老的小道上充满着迷离的格调,显得格外的幽静。天地万物似乎都酣醉在这片凝固着却极短暂的美丽之中,止住了一切声响。
读取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网页上拉一个RadioButtonList控件,用来显示XML的数据。
 
<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();
}
}
}

网页运行效果:

本文读取XML并绑定至RadioButtonList实现思路及演示动画到此结束。想去做的事情很多,所以我们得去努力,不只是想想而已。小编再次感谢大家对我们的支持!

您可能有感兴趣的文章
把字符串转为HtmlTable演示动画

FileUpload上传图片前实现图片预览功能(附演示动画)

为密码文本框要求不可粘帖字符串只可手动输入(附演示动画)

限制CheckBoxList控件只能单选实现代码及演示动画

Repeater控件数据导出Excel(附演示动画)