nested-xml-from-dataset.aspx

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

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

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

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

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

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

<%-- insert the control that creates the DataSet --%>
<wrox:getdataset id="ctlDataSet" runat="server"/>

<b>DataSet.Tables Collection</b>
<asp:datagrid id="dgrTables" runat="server" /><br />
<b>DataSet.Relations Collection</b>
<asp:datagrid id="dgrRelsNormal" runat="server" />
<div id="outResultNormal" runat="server" /><p />

<b>DataSet.Relations Collection</b>
<asp:datagrid id="dgrRelsNested" runat="server" />
<div id="outResultNested" runat="server" /><p />

<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;

		// create a variable to hold an instance of a DataSet object
		DataSet objDataSet;

		// get dataset from get-dataset-control.ascx user control
		objDataSet = ctlDataSet.BooksDataSet(strConnect, "ISBN LIKE '18610033%'");

		if (objDataSet == null)
			return;

		// now we// re ready to display the contents of the DataSet object
		// bind the collection of Tables to the first DataGrid on the page
		dgrTables.DataSource = objDataSet.Tables;
		dgrTables.DataBind();

		// bind the collection of Relations to the second DataGrid on the page
		dgrRelsNormal.DataSource = objDataSet.Relations;
		dgrRelsNormal.DataBind();

		// now we can save the DataSet contents to an XML disk file
		string strResult;

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

			// 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));
			outResultNormal.InnerHtml = "Written file: <b><a href='" + strVirtualPath
						+ "'>" + strVirtualPath + "</a></b>";
		}
		catch (Exception objError)
		{
			// display error details
			outResultNormal.InnerHtml = "<b>* Error while writing disk file</b>.<br />"
						+ objError.Message + "<br />" + objError.Source;
			return;  //  and stop execution
		}
		
		// now set the Nested property of the two relation objects
		objDataSet.Relations["BookAuthors"].Nested = true;
		objDataSet.Relations["BookPrices"].Nested = true;

		// bind the collection of Relations to the third DataGrid on the page
		dgrRelsNested.DataSource = objDataSet.Relations;
		dgrRelsNested.DataBind();

		// now save the DataSet contents to another XML disk file

		try
		{

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

			// 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));
			outResultNested.InnerHtml = "Written file: <b><a href='" + strVirtualPath
						+ "'>" + strVirtualPath + "</a></b>";
		}
		catch (Exception objError)
		{
			// display error details
			outResultNested.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 + -
显示快捷键?