To read XML documents, you can use the ReadXml () method in the System.Data.DataSet category. As the following XML document, placed under the root directory of the site:
Yearofbirth.xml
Copy Code code as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<YearOfBirths>
<YearOfBirth>
<ID>1</ID>
<Name> mice </Name>
</YearOfBirth>
<YearOfBirth>
<ID>2</ID>
<Name> Cattle </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> Sheep </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 a property to get this document:
Copy Code code as follows:
private String XmlFile
{
Get
{
Return Server.MapPath ("~/yearofbirth.xml");
}
}
Pull a RadioButtonList control on the ASPX page to display the XML data.
Copy Code code as follows:
<asp:radiobuttonlist id= "Radiobuttonlistyearofbirth" runat= "Server" repeatcolumns= "6" repeatdirection= " Horizontal "></asp:RadioButtonList>
Next, use the dataset to read the properties of the XML file that you just wrote.
Copy Code code 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 ();
}
}
}
Web page Run Effect: