Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 294
  • Last Modified:

Translate XML String into HTML string using XSL file

Hi,

I need to be able to translate an xml string using an xsl file and return the html output to a string for display.

I used to do the following in VB 6:

Private Function GetHTMLString(ByVal strXMLString  as String) as String
 
  Dim objXML as MSXML2.DOMDocument40
  Dim objXSL as MSXML2.DOMDocument40
  Set objXML = New MSXML2.DOMDocument40
  Set objXSL = New MSXML2.DOMDocument40

  strXMLString = "<?xml version='1.0'?><report><colour>red</colour></report>"
  objXML.loadXML (strXMLString)
  objXSL.Load ("c:\temp\test.xsl")

  GetHTMLString= objXML.transformNode(objXSL)

  Set objXML = Nothing
  Set objXSL = Nothing
End Function

Can anyone tell me how this is done in VB.Net - I have seen a lot of examples where the xml is translated from a file, but I don't want to actually use a file, probably just build up the XML string from a recordset and pass it into this function...

Thanks in advance...
R
0
rallsaldo
Asked:
rallsaldo
  • 3
  • 2
1 Solution
 
J_MakCommented:
0
 
rallsaldoAuthor Commented:
Hi,
Thanks,... I had looked at this link - however I am a bit puzzled as to how to simply use xml as a string and use the filepath of an xsl document....
This just seems to give me examples where the xml is loaded in from a file?... am I missing something? probably really obvious...

Would appreciate your help..

Thanks,
R
0
 
J_MakCommented:
Try something like this:

Private Function GetHTMLString(ByVal strXMLString  as String) as String
 
  Dim objXML as MSXML2.DOMDocument40
  Dim objXSL as MSXML2.DOMDocument40
  Set objXML = New MSXML2.DOMDocument40
  Set objXSL = New MSXML2.DOMDocument40

  strXMLString = "<report><colour>red</colour></report>"
  objXML.loadXML (strXMLString)
  objXSL.Load ("c:\temp\test.xsl")

  GetHTMLString= objXML.transformNode(objXSL)

  Set objXML = Nothing
  Set objXSL = Nothing
End Function

OTherwise try this:

Dim objXML as XmlDocument = new XmlDocument()
Dim objXSL as XslTransform = new XslTransform()

strXMLString = "<report><colour>red</colour></report>"
objXML.loadXML (strXMLString)
objXSL.Load ("c:\temp\test.xsl")

objXSL.Transform(doc, GetHTMLString)

Set objXML = Nothing
Set objXSL = Nothing

GetHTMLString would contain the filename of your html resutl. I have not tested it but it should give you a good start. Cheers.
0
 
rallsaldoAuthor Commented:
Hi, Thanks for this.... the second way is what I am after (the first is good but I don't want to have a reference to the MSXML2.DOMDocument40). However its the line:

objXSL.Transform(doc, GetHTMLString)

that I can't get to work... what is the doc reference?

Thanks for your help,
R
0
 
rallsaldoAuthor Commented:
This is what I was after but thanks for your help.


Public Function GetTransformedHTML(ByVal strXML As String, ByVal strXSLFile As String) As String
        Dim XMLDoc As New System.Xml.XmlDocument   'XPathDocument
        Dim XSLTDoc As XslTransform
        Dim swWriter As StringWriter = New StringWriter
        XMLDoc.LoadXml(strXML)
        XSLTDoc = New XslTransform
        XSLTDoc.Load(strXSLFile)
        XSLTDoc.Transform(XMLDoc, Nothing, swWriter)
        GetTransformedHTML = swWriter.ToString()
    End Function

Cheers,
R
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now