为了账号安全,请及时绑定邮箱和手机立即绑定

读取XML并绑定至RadioButtonList

标签:
Java

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


网页运行效果:

 

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消