multi-xsl-transform.aspx
来自「Professional ASP.NET source code」· ASPX 代码 · 共 104 行
ASPX
104 行
<%@Page Language="C#" debug="true"%>
<%@Import Namespace="System.Xml" %>
<%@Import Namespace="System.Xml.XPath" %>
<%@Import Namespace="System.Xml.Xsl" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head>
<title>Different ways to use the XSLTransform object</title>
<!-- #include file="..\global\style.inc" -->
</head>
<body bgcolor="#ffffff">
<span class="heading">Different ways to use the XSLTransform object</span><hr />
<!--------------------------------------------------------------------------->
<div id="outError" runat="server" /><p />
Using XML document file:
<b><span id="outXMLURL" runat="server" /></b><br />
Loaded XSL stylesheet file:
<b><span id="outXSLURL" runat="server" /></b><p />
Output from transformation is:
<div id="outResults" runat="server" /><p />
Transformed to disk file:
<b><span id="outFile" runat="server" /></b>
<script language="C#" runat="server">
void Page_Load(Object sender, EventArgs e)
{
// create virtual path to booklist.xml and authors.xsl files (in same folder as ASPX page)
string strCurrentPath = Request.Url.ToString();
string strXMLPath = strCurrentPath.Substring(0, strCurrentPath.LastIndexOf("/")) + "\\booklist.xml";
string strXSLPath = strCurrentPath.Substring(0, strCurrentPath.LastIndexOf("/")) + "\\authors.xsl";
string strOutPath = Request.PhysicalPath.Substring(0, Request.PhysicalPath.LastIndexOf("\\")) + "\\authors.xml";
try
{
// create a new XslTransform object to do the transformation
XslTransform objTransform = new XslTransform();
// load the XSL stylesheet into the XSLTransform object
objTransform.Load(strXSLPath);
outXSLURL.InnerHtml = "<a href='" + strXSLPath + "'>" + strXSLPath + "</a>";
// create a new XmlTextReader object to fetch XML document
XmlTextReader objXTReader = new XmlTextReader(strXMLPath);
outXMLURL.InnerHtml = "<a href='" + strXMLPath + "'>" + strXMLPath + "</a>";
// create a new XPathDocument object from the XmlTextReader
XPathDocument objXPDoc = new XPathDocument(objXTReader);
// create a new XPathNavigator object from the XPathDocument
XPathNavigator objXPNav;
objXPNav = objXPDoc.CreateNavigator();
// create a variable to hold the XmlReader object that is
// returned from the Transform method
XmlReader objReader;
// perform the transformation using the XSL file in the
// XSLTransform and the XML document referenced by the
// XPathNavigator. The result is in the XmlReader object
objReader = objTransform.Transform(objXPNav, null);
// display the contents of the XmlReader object
objReader.MoveToContent();
outResults.InnerText = objReader.ReadOuterXml();
// create an XMLTextWriter object to write result to disk
XmlTextWriter objWriter = new XmlTextWriter(strOutPath, null);
// write the opening <?xml .. ?> declaration and a comment
objWriter.WriteStartDocument();
objWriter.WriteComment("List of authors created " + DateTime.Now);
// transform the XML into the XMLTextWriter
objTransform.Transform(objXPNav, null, objWriter);
// ensure that all open elements are closed to end the document
objWriter.WriteEndDocument();
// flush the buffer to disk and close the file
objWriter.Close();
outFile.InnerHtml = "<a href='" + strOutPath + "'>" + strOutPath + "</a>";
}
// ------------ error handling code -------------
catch (Exception objError)
{
// display error details
outError.InnerHtml = "<b>* Error while accessing document</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 + -
显示快捷键?