This partial code sample shows you how to apply an XSL style sheet using C# or VB6.
C# Code Sample
References: Miner.Interop.msxml4
C# Snippet |
Copy Code
|
---|---|
/// <summary>> /// Transform an XML using the stylesheet passed in /// as of 12/2005, we recommend using msxml4 through an interop instead /// of System.XML, for better performance with large XMLs /// </summary>> /// <param name="sourceFile">file path for the source XML</param> /// <param name="styleSheet">file path for the XSL style sheet</param> /// <returns>transformed XML document</returns> public static Miner.Interop.msxml4.DOMDocument40Class TransformXML(string sourceFile, string styleSheet) { if (!File.Exists(sourceFile)) { throw new FileNotFoundException("the source XML file was not found",sourceFile); } if (!File.Exists(styleSheet)) { throw new FileNotFoundException("the stylesheet file was not found",styleSheet); } DOMDocument40Class resultXML = new DOMDocument40Class(); DOMDocument40Class styleSheetXML = new DOMDocument40Class(); DOMDocument40Class sourceXML = new DOMDocument40Class(); sourceXML.load(sourceFile); styleSheetXML.validateOnParse = false; styleSheetXML.resolveExternals = true; styleSheetXML.load(styleSheet); sourceXML.transformNodeToObject(styleSheetXML, resultXML); return resultXML; } |
VB6 Code Sample
References: msxml4.dll, scrrun.dll
Visual Basic Snippet |
Copy Code
|
---|---|
Private Function TransformXML(Original As DOMDocument40, XSLPath As String) As DOMDocument40 'transform an XML 'requires MS XML4 (msxml4.dll) and MS scripting runtime (scrrun.dll) Const k_DetailedErrorSource = "TransformXML" On Error GoTo eh Dim sMsg As String sMsg = "transforming xml" 'check the path Dim FSO As IFileSystem Set FSO = New FileSystemObject If Not FSO.FileExists(XSLPath) Then Err.Raise -1, k_DetailedErrorSource, _ "style sheet " + XSLPath + " not found" Exit Function End If Dim Result As DOMDocument40 Set Result = New DOMDocument40 Dim StyleSheet As DOMDocument40 Set StyleSheet = New DOMDocument40 StyleSheet.validateOnParse = False StyleSheet.resolveExternals = True Call StyleSheet.Load(XSLPath) Call Original.transformNodeToObject(StyleSheet, Result) Set TransformXML = Result GoTo finally eh: MsgBox k_DetailedErrorSource & vbCrLf & _ Err.Description & vbCrLf & _ "Line: " & CStr(Erl) finally: Set FSO = Nothing Set StyleSheet = Nothing End Function |