I have done my best to answer this question but can not find the
proper set of commands. I would like
to transform an xml file (in code behind) and display its output in a
specific html tag, such as a div with
a runat=server. I can somewhat do this if I create a server control
and include the control within the html
div tag but this method (borrowed from ASP.NET Website Programming by
Wrox press thanks guys) does not give me
the full control over the xml.
In my app I need to load different xml docs (depended on
selections from a previous screen). I have
the following html code:
----------------------------------------------------------------------
<%@ Page Language="vb"
AutoEventWireup="false"
Codebehind="QuestionnairePage.aspx.vb"
Inherits="Battelle.QuestionnairePage" enableViewState="True"
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Form Document</TITLE>
<meta content="Microsoft Visual Studio .NET 7.1"
name="GENERATOR">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET
7.1">
<meta name="vs_defaultClientScript"
content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
<link rel="stylesheet"
href="../Styles/QuestionStyle.css">
<script type="text/javascript" language="JavaScript"
src="../Config/Code/XMLactions.js" />
</HEAD>
<body MS_POSITIONING="GridLayout"
onload="formatUI(document.form1);">
<FORM id="form1" method="post" runat="server">
<DIV class="xml_Div" id="xmlData"
style="WIDTH: 100%; POSITION: absolute; HEIGHT: 100%"
runat="server"
MS_POSITIONING="GridLayout">
</DIV>
</FORM>
</body>
</HTML>
----------------------------------------------------------------------
The folling vb.NET code:
----------------------------------------------------------------------
Imports System.IO
Imports System.Text
Imports System.Xml
Imports System.Xml.Xsl
Imports System.Xml.XPath
Public Class QuestionnairePage
Inherits System.Web.UI.Page
Protected WithEvents xmlSourceBox As
System.Web.UI.HtmlControls.HtmlInputHidden
Protected WithEvents xsltSourceBox As
System.Web.UI.HtmlControls.HtmlInputHidden
Protected WithEvents xmlData As
System.Web.UI.HtmlControls.HtmlGenericControl
Dim _err As Exception
#Region " Web Form Designer Generated Code "
--code takenout to save space.
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If (Not IsPostBack) Then
Dim DocID As Integer =
CInt(Trim(Request.QueryString("DocID")))
Dim DocVersion As Double =
CDbl(Trim(Request.QueryString("Ver")))
Dim EmployeeID As String =
UCase(Trim(Request.QueryString("EmpID")))
Dim xDoc As New XmlDocument
Dim xslt As New XslTransform
If EmployeeID.Length = 0 Then
'get data
retrieveMasterDocument(xDoc, xslt, DocID, DocVersion)
Else
'get data
retrieveUserDocument(xDoc, xslt, DocID, DocVersion,
EmployeeID)
End If
'display xml
displayXML(xDoc, xslt)
End If
End Sub
Private Sub displayXML(ByRef xDoc As XmlDocument, ByRef xslt As
XslTransform)
Dim resolver As XmlUrlResolver = New XmlUrlResolver
resolver.Credentials =
System.Net.CredentialCache.DefaultCredentials
Dim xmlStream As Stream
' Transform the file.
'Get error here because xmlStream is Nothing.
xslt.Transform(xDoc, Nothing, xmlStream, resolver)
Dim htmlText As String = New
StreamReader(xmlStream).ReadToEnd()
xmlData.InnerHtml = htmlText
'This replaces all html on the page.
'xslt.Transform(xDoc, Nothing, Page.Response.Output, Nothing)
End Sub
Private Sub pageError(ByRef err As Exception)
Try
If err.Message.Length > 0 Then
Response.StatusDescription = err.Message
Response.Redirect("../Screens/RunTimeError.aspx")
End If
Catch ex As Exception
End Try
End Sub
Private Sub retrieveMasterDocument(ByRef xDoc As XmlDocument,
ByRef xslt As XslTransform, ByVal docID As Integer, ByVal version As
Double)
Dim _xsltPath As String = "../Styles/Question.xslt"
xslt.Load(context.Server.MapPath(_xsltPath))
Dim data As New
DataRetrieval(ConfigurationSettings.AppSettings("A nsweredQuestions_LOCAL"))
xDoc = data.selectMasterDocument(docID, version, _err)
pageError(_err)
End Sub
Private Sub retrieveUserDocument(ByRef xDoc As XmlDocument, _
ByRef xslt As XslTransform, _
ByVal docID As Integer, _
ByVal version As Double, _
ByVal emplID As String)
Dim _xsltPath As String = "../Styles/Question.xslt"
xslt.Load(context.Server.MapPath(_xsltPath))
Dim data As New
DataRetrieval(ConfigurationSettings.AppSettings("A nsweredQuestions_LOCAL"))
xDoc = data.selectAnsweredDocument(emplID, docID, version,
_err)
pageError(_err)
End Sub
End Class
----------------------------------------------------------------------
The sub displayXML tries to use a stream object to capture the
transformed
xml and then convert that stream into a string then into the
..innerHTML.
I am getting an error message because the stream object is Nothing.
My take is that
I should not be using the stream this way. Anyone have any ideas of
how to
transform an xml into a string so it can be placed surgically into the
html page?