data7.xml:
<?xml version="1.0" encoding="utf-8"?>
<Advertisements>
<Ad>
<ImageUrl>001.jpg</ImageUrl>
<NavigateUrl>001.aspx</NavigateUrl>
<Impressions>10</Impressions>
<Keyword>gucas01</Keyword>
</Ad>
<Ad>
<ImageUrl>002.jpg</ImageUrl>
<NavigateUrl>002.aspx</NavigateUrl>
<Impressions>20</Impressions>
<Keyword>gucas02</Keyword>
</Ad>
<Ad>
<ImageUrl>003.jpg</ImageUrl>
<NavigateUrl>003.aspx</NavigateUrl>
<Impressions>30</Impressions>
<Keyword>gucas03</Keyword>
</Ad>
<Ad>
<ImageUrl>004.jpg</ImageUrl>
<NavigateUrl>004.aspx</NavigateUrl>
<Impressions>40</Impressions>
<Keyword>gucas04</Keyword>
</Ad>
<Ad>
<ImageUrl>005.jpg</ImageUrl>
<NavigateUrl>005.aspx</NavigateUrl>
<Impressions>50</Impressions>
<Keyword>gucas05</Keyword>
</Ad>
<Ad>
<ImageUrl>google.gif</ImageUrl>
<NavigateUrl>http://www.google.com</NavigateUrl>
<Impressions>30</Impressions>
<Keyword>google</Keyword>
</Ad>
</Advertisements>
后端:
protected void Page_Load(object sender, EventArgs e)
{
DataSet objDataSet = new DataSet();
objDataSet.ReadXml(Server.MapPath("data7.xml"));
//方法1
//dgEmployees.DataSource = objDataSet;
//dgEmployees.DataBind();
//方法2
dgEmployees.DataSource = objDataSet.Tables["Ad"].DefaultView;
dgEmployees.DataBind();
}
转载自:https://blog.csdn.net/xw13106209/article/details/6367865
------------------------
实践补充:
/// <summary>
/// 可以转为xml文件保存起来
/// </summary>
/// <param name="mydt">datatable(必须指定TableName,否则转换失败)</param>
/// <param name="Filename">指定XML文件名</param>
public void DataTableToXmlFile(DataTable mydt, string Filename)
{
mydt.WriteXml(System.AppDomain.CurrentDomain.BaseDirectory + "\\" + Filename, XmlWriteMode.WriteSchema);
}
/// <summary>
/// 把xml文件转换成DataTable
/// </summary>
/// <param name="Filename"></param>
/// <returns></returns>
public DataTable XmlFileToDataTable(string Filename)
{
DataSet objDataSet = new DataSet();
objDataSet.ReadXml(System.AppDomain.CurrentDomain.BaseDirectory + "\\" + Filename);
return objDataSet.Tables[0];
}
/ <summary>
/ 可以转为xml文件保存起来
/ </summary>
/ <param name="mydt">datatable(必须指定TableName,否则转换失败)</param>
/ <param name="Filename">指定XML文件名</param>
//private void DataTableToXmlFile(DataTable mydt, string Filename)
//{
// var CheckDataFile = System.AppDomain.CurrentDomain.BaseDirectory + "\\" + "CheckDataFile";
// if (!Directory.Exists(CheckDataFile))
// {
// Directory.CreateDirectory(CheckDataFile);
// }
// mydt.WriteXml(CheckDataFile + "\\" + Filename, XmlWriteMode.WriteSchema);
//}
/ <summary>
/ 把xml文件转换成DataTable
/ </summary>
/ <param name="Filename"></param>
/ <returns></returns>
//private DataTable XmlFileToDataTable(string Filename)
//{
// var CheckDataFile = System.AppDomain.CurrentDomain.BaseDirectory + "\\" + "CheckDataFile";
// if (!Directory.Exists(CheckDataFile))
// {
// Directory.CreateDirectory(CheckDataFile);
// }
// DataSet objDataSet = new DataSet();
// objDataSet.ReadXml(CheckDataFile + "\\" + Filename);
// return objDataSet.Tables[0];
//}