read-data-from-xml.aspx

来自「Professional ASP.NET source code」· ASPX 代码 · 共 76 行

ASPX
76
字号
<%@Page Language="C#"%>

<%@Import Namespace="System.Data" %>

<%@ Register TagPrefix="wrox" TagName="connect" Src="..\global\connect-strings.ascx" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head>
<title>Reading Data into a DataSet from an XML File</title>
<!-- #include file="..\global\style.inc" -->
</head>
<body bgcolor="#ffffff">
<span class="heading">Reading Data into a DataSet from an XML File</span><hr />
<!--------------------------------------------------------------------------->

<div id="outMessage" runat="server"></div><br />
<b>DataSet.Tables Collection</b>
<asp:datagrid id="dgrTables" runat="server" /><br />
<b>Contents of DataSet.Tables("Books")</b>
<asp:datagrid id="dgrValues" runat="server" />

<script language="C#" runat="server">

	void Page_Load(Object sender, EventArgs e)
	{

		// create a new DataSet object
		DataSet objDataSet = new DataSet();

		try
		{
			// use the path to the current virtual application
			string strVirtualPath = "XML-from-DataSet.xml";
			string strVSchemaPath = "Schema-from-DataSet.xsd";

			// read the schema and data into the DataSet from an XML document on disk
			// must use the Physical path to the file not the Virtual path
			objDataSet.ReadXmlSchema(Request.MapPath(strVSchemaPath));
			outMessage.InnerHtml = "Reading file: <b><a href='" + strVSchemaPath
					+ "'>" + strVSchemaPath + "</a></b><br />";

			objDataSet.ReadXml(Request.MapPath(strVirtualPath));
			outMessage.InnerHtml += "Reading file: <b><a href='" + strVirtualPath
					+ "'>" + strVirtualPath + "</a></b>";
		}
		catch (Exception objError)
		{
			// display error details
			outMessage.InnerHtml = "<b>* Error while reading disk file</b>.<br />"
							+ objError.Message + "<br />" + objError.Source + "<br />"
							+ "Run the previous example to create the disk file";
			return;  //  and stop execution
		}

		// now we can display the DataSet contents

		// assign the DataView.Tables collection to first the DataGrid control
		dgrTables.DataSource = objDataSet.Tables;
		dgrTables.DataBind();  //and bind (display) the data

		// create a DataView object for the Books table in the DataSet
		DataView objDataView = new DataView(objDataSet.Tables["Books"]);

		// assign the DataView object to the second DataGrid control
		dgrValues.DataSource = objDataView;
		dgrValues.DataBind();  // and bind (display) the data

	}
</script>


<!--------------------------------------------------------------------------->
<!-- #include file="..\global\foot.inc" -->
</body>
</html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?