VB2005 Using XSL files in my.resources

CForsyth

Member
Joined
Jun 7, 2006
Messages
8
Programming Experience
Beginner
Can anyone give me some example code of how to use an XSLT file stored in my.resources?

What I'm trying to do is use the XslCompiledTransform Class to transform XML documents using an XSLT stylesheet saved in my.resources.

What I'm trying to prevent having to deploy both the exe and an .xslt file.

I'm assuming that moving the file into the My.Resources area by dragging it in there from the My Project area will compile the file within the exe. If I'm way off base here please let me know and please suggest alternatives.

Thanks in advance. I hope this isn't too advanced a question for this thread.
 
Storing the Xslt file in resources is fine. When you need the file the easiest solution is to write the resource to file and use the XslCompiledTransform.Load (String) where String is path of file. You may also use the XslCompiledTransform.Load (XmlReader) where you first read the resource into a XmlTextReader, which in turn needs either a file location or another stream. In my opinion it now boils down a IO.MemoryStream. By default plain text file resources like the Xslt is stored as a string, it is a bit cumbersome to get the string into stream, so instead go into resources and select the file then change its FileType property from 'Text' to 'Binary'. Below are two code examples first is reading the resource as binary into MemoryStream, then into XmlTextReader which in turn is loaded into XslCompiledTransform.Load method. My file was 'display.xsl' so the resource is 'My.Resources.display'.
VB.NET:
Dim mem As New IO.MemoryStream(My.Resources.display) 'display is here binary resource
Dim xr As New Xml.XmlTextReader(mem)
Dim xsl As New Xml.Xsl.XslCompiledTransform()
xsl.Load(xr)
If you decide to stick with the resource as string, you do this utilizing a IO.StreamWriter, like I said a little more cumbersome:
VB.NET:
Dim mem As New IO.MemoryStream
Dim sw As New IO.StreamWriter(mem)
sw.Write(My.Resources.display) 'display is here string resource
sw.Flush()
mem.Seek(0, IO.SeekOrigin.Begin)
Dim xr As New Xml.XmlTextReader(mem)
Dim xsl As New Xml.Xsl.XslCompiledTransform()
xsl.Load(xr)
Not sure if you need it, but I tested a Transform also with this code afterwards:
VB.NET:
Dim xmlfile As String = Application.StartupPath & "\Data.xml"
Dim output As String = Application.StartupPath & "\Presentation.html"
xsl.Transform(xmlfile, output)
' and close the streams that was used
[SIZE=2]mem.Close()
xr.Close()
sw.Close() '(if used)
[/SIZE]
 
About actually adding the file to the application resources, you can't just put the file in the Resources folder. You have to go to Project Properties, the Resources tab, select type File, click Add Resource and browse to file. This is also where you may change the FileType of that resource from 'Text' to 'Binary' as mentioned in the post above. When in Resource tab you can also just paste the file there without need to select & browse.
 
p.s. Why didn't Microsoft just add another overloaded XslCompiledTransform.Load method that accepts an XMLDocument object as an argument?

It would solve all my problems. :)
 
Would this work?

VB.NET:
Dim mem As New IO.MemoryStream(My.Resources.XSLFileData) 
Dim xr As XmlReader
xr = XmlReader.Create(mem)
Dim xsl As New Xml.Xsl.XslCompiledTransform()
xsl.Load(xr)
 
CForsyth said:
does the XMLCompiledTransform .Load method accept an XMLTextReader as an argument?
XmlTextReader is derived from XmlReader so that works. Your suggestion with XmlReader.Create is perfectly valid.
 
JohnH said:
XmlTextReader is derived from XmlReader so that works. Your suggestion with XmlReader.Create is perfectly valid.

HOLY COW! I was so clueless! :)

So, if what you are saying is true, then this should work...

VB.NET:
Dim myXSLDoc As XmlDocument = New XmlDocument()
myXSLDoc.LoadXml(My.Resources.XSLTFile)
 
Dim xsl As New Xml.Xsl.XslCompiledTransform()
xsl.Load(myXSLDoc)


Since the XsLCompiledTransform.Load method accepts an IXPathNavigable type and the XMLDocument impliments the IXPathNavigable interface.

Could it be true?


-Charles
 
You got it! :) And your resource Filetype must be in 'Text' mode when used with LoadXml since it only accept string input. I didn't check the documentation well enough to answer your question about this earlier - good job!
 
JohnH said:
You got it! :) And your resource Filetype must be in 'Text' mode when used with LoadXml since it only accept string input. I didn't check the documentation well enough to answer your question about this earlier - good job!

You helped me more than you know! Thank you.

Don't worry about the "over-complicated" first idea. Most ideas are that way, it takes time to refine them. :)

If you hadn't replied I would still be stuck on this, and probably stuck for many more hours too.
 
Ok, new problem now. hehe

How do I get the results of the XSLCompiledTransform.Transform method into an XML.XMLDocument object?

The XSL stylesheet I created converts one version of XML document to another version of XML document. This way I only need to write processing instructions in VB to parse out one version of XML document.

After I convert the XML document and I have it in an XMLDocument object I plan to start writing the code to parse out the parts I need and store them to a database.
 
It's me and the MemoryStream again...
VB.NET:
Dim mem As New IO.MemoryStream
xsl.Transform(xmlfile, Nothing, mem)
mem.Seek(0, IO.SeekOrigin.Begin)
Dim xdocout As New Xml.XmlDocument
xdocout.Load(mem)
 
Back
Top