RE: using myXslDoc.Transform

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hi Aaron,
Thanks for all your help. I really appreciate it

Below is the xsl file
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp " "> ]>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";><xsl:template match="/">
<html>

<body>



<xsl:for-each select="head/something">
<table width="100%">
<tr>
<td align="left"><img src="../images/test.gif" alt="+" border="0"/></td>

</tr>

</table>
<table width="100%">
<tr>
<td>
<p>[10/20/2006]</p>
<p> </p>
<p><xsl:value-of select="FirstName"/><xsl:value-of select="LastName"/></p>

</td>
</tr>

</table>


</xsl:for-each>
</body>
</html>
</xsl:template></xsl:stylesheet>
and xml file

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<head>
<something>
<CSRFirst>Test</CSRFirst>
<CSRLast>person</CSRLast>
<FirstName>test1</FirstName>
<LastName>person2</LastName>
<faxNum>(213)244-2178</faxNum>
<faxAtten>M</faxAtten>
<LtrRE>JONATHAN MOPPINS</LtrRE>
<Add1>123 test ave.</Add1>

</something>


</head>
--
vinki


"Aaron Dunnington [MSFT]" wrote:

Thanks Vinki.

If you have a moment, can you send on the XML and XSL that are involved in
the transform when the runtime error occurs?

Thanks,
Aaron

"vinki" wrote:

Hi Aaron,

Below is my code

Private Shared Sub MergeCoverSheet(ByVal indexId As Integer)
Dim cn As SqlConnection = New
SqlConnection(ConfigurationSettings.AppSettings("connectionStringOA"))
'get xml file from the database and load xml file
Dim strXML As String = SqlHelper.ExecuteScalar(cn,
CommandType.StoredProcedure, "sp_UnprintedLetterCover", New
SqlParameter("@IndexId", indexId))
Dim myXmlDoc As XmlDocument = New XmlDocument
Dim documentResolver As XmlResolver
myXmlDoc.LoadXml(strXML)
' get xsl file from database and load xsl file
Dim strXSL As String = SqlHelper.ExecuteScalar(cn,
CommandType.StoredProcedure, "sp_xsltDocument", New SqlParameter("@name",
"Letter Cover Sheet "))
Dim myXslDoc As XslTransform = New XslTransform
' Dim strXslFile As String = "CoverSheet.xsl"
myXslDoc.Load(New XmlTextReader(New StringReader(strXSL)), Nothing,
Nothing)
' myXslDoc.Load(strXslFile)
Dim myStringBuilder As New StringBuilder
Dim myStringWriter As New StringWriter(myStringBuilder)
' perform the transformation
myXslDoc.Transform(myXmlDoc, Nothing, myStringWriter,
documentResolver)
printDocument(myStringWriter.ToString())


End Sub

Thanks,
--
vinki


"Aaron Dunnington [MSFT]" wrote:

Hi vinki,

If you have a moment, could you attach the updated source file where this
method is being called?

Thanks!
Aaron

"vinki" wrote:

Hi Aaron,

myXslDoc.Load(New XmlTextReader(New StringReader(strXSL)))

I tried this command and it didn't work. I was giving syntax error "you
should pass evidence to load method". I rewrote the above statement this way
myXslDoc.Load(New XmlTextReader(New StringReader(strXSL)), Nothing, Nothing)
Syntax error was gone, but while debugginbg it broke on this statement and I
got an error " operation is not valid due to the current state of the
object". " An unhadled exception of type 'system.InvalidOperationException'
occured in system.xml.dll.

I am using .net framework 1.1

Please let me know what am I doing wrong.
--
vinki


"Aaron Dunnington [MSFT]" wrote:

The System.Drawing.Printing namespace provides printing facilities.

Please see the following location for some examples:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdrawingprintingprintdocumentclasstopic.asp


Thanks!
Aaron

--
This posting is provided "AS IS" with no warranties, and confers no rights.
--


"Vinki" wrote:

Hi Aaron,

Thanks for your response. I was just wondering that is it possible to send
the transformed/Merged file directly to a printer.

Thanks,

Vinki

"Aaron Dunnington [MSFT]" wrote:

Hi Nitu,

Thanks for your response.

Given a string of XSL, you can load the XslTransform with an XmlReader.

myXslDoc.Load(New XmlTextReader(New StringReader(strXSL)))

Then, the transform can be applied as usual.

If you have any questions, comments, or concerns, please don't hesitate to
reply to this post.

Thanks,
Aaron

--
This posting is provided "AS IS" with no warranties, and confers no rights.
--

"vinki" wrote:

Hi Aaron,

Thanks for your help. Below is my code. Only thing is my xslt file is also
coming from database. There is another application that is inserting the xslt
file in the database. I want to load strXSL here. myXslDoc.Load(strXslFile).
How can I do that.
Thanks,

Nitu

Dim cn As SqlConnection = New
SqlConnection(ConfigurationSettings.AppSettings("connectionStringOA"))
'get xml file from the database and load xml file
Dim strXML As String = SqlHelper.ExecuteScalar(cn,
CommandType.StoredProcedure, "sp_UnprintedLetterCover", New
SqlParameter("@IndexId", indexId))
Dim myXmlDoc As XmlDocument = New XmlDocument
Dim documentResolver As XmlResolver
myXmlDoc.LoadXml(strXML)
' get xsl file from database and load xsl file
Dim strXSL As String = SqlHelper.ExecuteScalar(cn,
CommandType.StoredProcedure, "sp_xsltDocument", New SqlParameter("@name",
"Letter Cover Sheet "))
Dim myXslDoc As XslTransform = New XslTransform
Dim strXslFile As String = "CoverSheet.xsl"
myXslDoc.Load(strXslFile)
Dim myStringBuilder As New StringBuilder
Dim myStringWriter As New StringWriter(myStringBuilder)
' perform the transformation
myXslDoc.Transform(myXmlDoc, Nothing, myStringWriter,
documentResolver)
Dim str As String = myStringWriter.ToString()
--
vinki


"Aaron Dunnington [MSFT]" wrote:

Hi Vinki,

Thanks for using our products!

One potential option for dynamically loading an XmlDocument from the
database would be to use the LoadXml method of XmlDocument with XML content
retrieved as a string.

For this example, here are the steps that we’ll follow to dynamically load
the XmlDocument from the database and apply a transform:

1. Retrieve the XML content from the database
2. Load the XML content into an XmlDocument with LoadXml
3. Apply the XSL transform

The code for this example is listed below.

Also, there are a couple of other options for loading the XmlDocument. For
example, if your data is not originally stored as XML, you can use FOR XML in
your SQL query with the ExecuteXmlReader method of SqlCommand to return an
XmlReader to feed into the XmlDocument.

If you have any questions, comments, or concerns, please don’t hesitate to
reply to this posting.

Thanks!
Aaron


Dynamically Loading the XmlDocument
--
Imports System.Xml
Imports System.Xml.Xsl
Imports System.Data.SqlClient
Imports System.IO
Imports System.Text

Module Driver

Sub Main()

' For this example, the XML content is stored as an nvarchar(255)

Dim strConnection As String = "CONNECTION_STRING_HERE"
Dim strQuery As String = "SELECT XmlContent FROM XmlContent WHERE
XmlContentID = 1"
Dim strXml As String

Dim connection As SqlConnection = New SqlConnection(strConnection)

Try

' Open the database connection
connection.Open()

' Create a command
Dim command As SqlCommand = New SqlCommand(strQuery, connection)

' Here we're replacing the Server.MapPath call with the results
of the SQL query
'Dim strXMLFile As String = Server.MapPath("xmlData.xml")

strXml = Convert.ToString(command.ExecuteScalar())

Catch ex As SqlException

' An error occured, write message and return
Console.WriteLine(ex.Message)
Return

Finally

' If the connection exists, close it
If Not connection Is Nothing Then
connection.Close()
End If

End Try

'
' Load the source XML into an XmlDocument with LoadXml
'
Dim myXmlDoc As XmlDocument = New XmlDocument
myXmlDoc.LoadXml(strXml)

' Load the XSL
Dim strXslFile As String = ".\greeting.xslt"
Dim myXslDoc As XslTransform = New XslTransform
myXslDoc.Load(strXslFile)

Dim myStringBuilder As StringBuilder = New StringBuilder
Dim myStringWriter As StringWriter = New StringWriter(myStringBuilder)

'
' Perform the transformation
'
' The source XML from the database is:
' =========================================
' <?xml version="1.0" encoding="utf-8"?>
' <ns:greeting xmlns:ns="urn:greeting:v1">
' <ns:message>Hello World</ns:message>
' </ns:greeting>
myXslDoc.Transform(myXmlDoc, Nothing, myStringWriter)

' Write the result of the transformation
Console.WriteLine(myStringWriter.ToString())
End Sub

End Module


Source XML Content
--
<?xml version="1.0" encoding="utf-8"?>
<ns:greeting xmlns:ns="urn:greeting:v1">
<ns:message>Hello World</ns:message>
</ns:greeting>


greeting.xslt
--
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:ns="urn:greeting:v1">
<xsl:template match="/">
<html>
<body>
<h1>Greeting</h1>
<p>
<xsl:value-of select="ns:greeting/ns:message"/>
.



Relevant Pages

  • RE: ASP.Net app cannot read XML with Anonymous Authentication disabled
    ... ASP.Net app cannot read XML with Anonymous Authentication ... | Dim ProjReader As XmlReader = Nothing ... | Dim ProjURL As String = Session ... | 'Get XML Data for Projector and load in Projector Dropdown ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • RE: ASP.Net app cannot read XML with Anonymous Authentication disabled
    ... ASP.Net app cannot read XML with Anonymous Authentication ... | Dim ProjReader As XmlReader = Nothing ... | Dim ProjURL As String = Session ... | 'Get XML Data for Projector and load in Projector Dropdown ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • Net.WebRequest/WebResponse ?s
    ... We have some legacy code which makes use of the MS XML Core Services and needed to be upgraded/updated. ... Public Function GetResponsePWP(ByVal ReqURL As String, ByVal ReqTimeout As Integer, ByVal ReqType As String, ByVal PostData As String, ByVal UserName As String, ByVal Password As String) As String ... Dim DataStream As Stream ... nmsStatusDescr = HTTPResponse.StatusDescription ...
    (microsoft.public.dotnet.languages.vb)
  • RE: using myXslDoc.Transform
    ... 'get xml file from the database and load xml file ... Dim strXML As String = SqlHelper.ExecuteScalar(cn, ... the transform can be applied as usual. ...
    (microsoft.public.dotnet.xml)
  • RE: using myXslDoc.Transform
    ... "vinki" wrote: ... Dim strXML As String = SqlHelper.ExecuteScalar(cn, ... ' get xsl file from database and load xsl file ...
    (microsoft.public.dotnet.xml)