Read XML and bind it to RadioButtonList implementation and demonstration Animation

Source: Internet
Author: User

To read XML documents, you can use the ReadXml () method in the System. Data. DataSet category. Put the following xml document under the root directory of the site:
YearOfBirth. xml
Copy codeThe Code is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?>
<YearOfBirths>
<YearOfBirth>
<ID> 1 </ID>
<Name> rat </Name>
</YearOfBirth>
<YearOfBirth>
<ID> 2 </ID>
<Name> Niu </Name>
</YearOfBirth>
<YearOfBirth>
<ID> 3 </ID>
<Name> tiger </Name>
</YearOfBirth>
<YearOfBirth>
<ID> 4 </ID>
<Name> rabbit </Name>
</YearOfBirth>
<YearOfBirth>
<ID> 5 </ID>
<Name> dragon </Name>
</YearOfBirth>
<YearOfBirth>
<ID> 6 </ID>
<Name> snake </Name>
</YearOfBirth>
<YearOfBirth>
<ID> 7 </ID>
<Name> horse </Name>
</YearOfBirth>
<YearOfBirth>
<ID> 8 </ID>
<Name> Yang </Name>
</YearOfBirth>
<YearOfBirth>
<ID> 9 </ID>
<Name> monkey </Name>
</YearOfBirth>
<YearOfBirth>
<ID> 10 </ID>
<Name> chicken </Name>
</YearOfBirth>
<YearOfBirth>
<ID> 11 </ID>
<Name> dog </Name>
</YearOfBirth>
<YearOfBirth>
<ID> 12 </ID>
<Name> pig </Name>
</YearOfBirth>
</YearOfBirths>

Use an attribute to obtain this document:
Copy codeThe Code is as follows:
Private string XmlFile
{
Get
{
Return Server. MapPath ("~ /YearOfBirth. xml ");
}
}

Pull a RadioButtonList control on the aspx page to display XML data.
Copy codeThe Code is as follows:
<Asp: RadioButtonList ID = "RadioButtonListYearOfBirth" runat = "server" RepeatColumns = "6" RepeatDirection = "Horizontal"> </asp: RadioButtonList>

Next, use DataSet to read the attributes of the XML file you just wrote.
Copy codeThe Code is as follows:
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 ();
}
}
}

Webpage running effect:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.