RE: using myXslDoc.Transform
- From: Aaron Dunnington [MSFT] <AaronDunningtonMSFT@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 31 Oct 2006 22:48:01 -0800
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:style*** 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"/>
</p>
</body>
</html>
</xsl:template>
</xsl:style***>
Transformation Result
--
<html xmlns:ns="urn:greeting:v1">
<body>
<h1>Greeting</h1>
<p>Hello World</p>
</body>
</html>
--
This posting is provided "AS IS" with no warranties, and confers no rights.
"vinki" wrote:
Hi Everyone,.
I am using myXslDoc.Transform. I have the following code to use this
Dim strXMLFile As String = Server.MapPath("xmlData.xml")
Dim documentResolver As XmlResolver
Dim strXslFile As String = Server.MapPath("xslData.xsl")
Dim myXmlDoc As XmlDocument = New XmlDocument
myXmlDoc.Load(strXMLFile)
Dim myXslDoc As XslTransform = New XslTransform
myXslDoc.Load(strXslFile)
Dim myStringBuilder As StringBuilder = New StringBuilder
Dim myStringWriter As StringWriter = New StringWriter(myStringBuilder)
myXslDoc.Transform(myXmlDoc, Nothing, myStringWriter,
documentResolver)
I am creating xmlData.xml file dynamically using sql statement. I was
wondering how can I replace server.mapPath and load my dynamically created
xml file in myXmlDoc.Load( xmlfilestring).
Any help will be appreciated
--
vinki
- Follow-Ups:
- RE: using myXslDoc.Transform
- From: vinki
- RE: using myXslDoc.Transform
- Prev by Date: Re: 2 XML nodes, 1 xslt
- Next by Date: Re: 2 XML nodes, 1 xslt
- Previous by thread: Re: 2 XML nodes, 1 xslt
- Next by thread: RE: using myXslDoc.Transform
- Index(es):
Loading