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: