从XML中读取数据!

网络整理 - 09-14
1.建立一个xml数据文件(artists.xml),就象sql一样的包含字段名和数据行,代码如下:

<?xml version="1.0" encoding="gb2312"?>
<artists>
<artist>
<name>Clark Fang</name>
<nationality>China</nationality>
<movement>IT SoftDevolepment</movement>
<birthday>Sep. 22ed 1981</birthday>
</artist>
<artist>
<name>Baby Liu</name>
<nationality>China</nationality>
<movement>IT SiteDesigner</movement>
<birthday>Dec. 12ed 1977</birthday>
</artist>
<artist>
<name>Summer Yao</name>
<nationality>China</nationality>
<movement>IT Softdevelopment</movement>
<birthday>Aug. 3rd 1981</birthday>
</artist>
<artist>
<name>Ziye</name>
<nationality>China</nationality>
<movement>IT SoftDevolepment</movement>
<birthday>Aug. 2rd 1981</birthday>
</artist>
<artist>
<name>Bob</name>
<nationality>China</nationality>
<movement>Manager</movement>
<birthday>Feb. 22rd 1979</birthday>
</artist>
</artists>

简单吧,不难看出,在<artist></artist>中间就是一个对象的属性(数据)!

2.编写一个asp.net文件(XMLshow.aspx),从xml上面提取数据到DataGrid上面显示,代码如下:

<%@ Page Language="C#" Debug="true"%>
<%@ Import Namespace="System.Data"%>
<%@ Import NameSpace="System.Xml"%>
<script runat="server">
void Page_Load(Object o,EventArgs e)
{
string str_xml=Server.MapPath("artists.xml");
DataSet ds=new DataSet();
//从xml中读取数据
ds.ReadXml(str_xml);
DG.DataSource=ds;
DG.DataBind();
}
</script>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Xml DataGrid</title>
</head>
<body>
<asp:DataGrid ID="DG" runat="server"/>
</body>
</html>

完了!!谢谢大家支持!!希望能得到学长们的支持!!