write-data-as-xml.aspx

来自「东软内部材料(四)asp等相关的教学案例 」· ASPX 代码 · 共 99 行

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

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

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

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

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

<%-- insert connection string script --%>
<wrox:connect id="ctlConnectStrings" runat="server" />

<div>Connection string: <b><span id="outConnect" runat="server"></span></b></div>
<div>SELECT command: <b><span id="outSelect" runat="server"></span></b></div><p />
<div id="outMessage" runat="server">&nbsp;</div>

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

	void Page_Load(Object sender, EventArgs e)
	{

		// get connection string from ..\global\connect-strings.ascx user control
		string strConnect = ctlConnectStrings.OLEDBConnectionString;
		outConnect.InnerText = strConnect; // and display it

		// specify the SELECT statement to extract the data
		string strSelect = "SELECT BookList.*, BookAuthors.FirstName, BookAuthors.LastName "
								+ "FROM BookList INNER JOIN BookAuthors ON BookList.ISBN = BookAuthors.ISBN "
								+ "WHERE BookList.ISBN LIKE '18610033%'";
		outSelect.InnerText = strSelect;   // and display it

		// declare a variable to hold a DataSet object
		DataSet objDataSet = new DataSet();

		try
		{
			// create a new OleDbConnection object using the connection string
			OleDbConnection objConnect = new OleDbConnection(strConnect);

			// create a new OleDbDataAdapter using the connection object and select statement
			OleDbDataAdapter objDataAdapter = new OleDbDataAdapter(strSelect, objConnect);

			// fill the dataset with data from the DataSetCommand object
			objDataAdapter.Fill(objDataSet, "Books");
		}
		catch (Exception objError)
		{
			// display error details
			outMessage.InnerHtml = "<b>* Error while accessing data</b>.<br />"
						+ objError.Message + "<br />" + objError.Source;
			return;  //  and stop execution
		}
		

		// now we're ready to save the DataSet contents to an XML disk file

		try
		{

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

			// write the data and schema from the DataSet to an XML document on disk
			// must use the Physical path to the file not the Virtual path
			objDataSet.WriteXml(Request.MapPath(strVirtualPath));
			outMessage.InnerHtml = "Written file: <b><a href='" + strVirtualPath
					+ "'>" + strVirtualPath + "</a></b><br />";
			objDataSet.WriteXmlSchema(Request.MapPath(strVSchemaPath));
			outMessage.InnerHtml += "Written file: <b><a href='" + strVSchemaPath
					+ "'>" + strVSchemaPath + "</a></b>";
		}
		catch (Exception objError)
		{
			// display error details
			outMessage.InnerHtml = "<b>* Error while writing disk file</b>.<br />"
							+ objError.Message + "<br />" + objError.Source;
			return;  //  and stop execution
		}
		

	}
</script>


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

⌨️ 快捷键说明

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