Need to add assembly reference to an ASP.NET page with XML/XSL transform

G

Greg Collins [MVP]

I have an ASP.NET page that uses a tag:

<asp:Xml id="foo" runat="server" DocumentSource="rss.xml" TransformSource="rss20.xsl" />

This creates a Web page from an XML file that was generated by InfoPath. By default InfoPath inline's their images in base64 encoding. These need to be extracted out to be displayed.

I've got my XSL set up to be able to pull out images and manage them separately. According to Microsoft, they need to be decoded and returned back as an image stream to the src attribute of the img tag.

See: http://www.mcse.ms/archive180-2004-4-605843.html
See: http://blogs.msdn.com/infopath/archive/2004/04/21/117600.aspx

So I've got some C# code in my XSL that performs this decode operation -- well, that's where I need help. Here's the code:

<msxsl:script language="C#" implements-prefix="cs">
<![CDATA[
Image decode(string s)
{
byte[] image = Convert.FromBase64String( s );
System.IO.MemoryStream memStr = new System.IO.MemoryStream();
memStr.Write( image, 0, image.Length );
System.Drawing.Image img = System.Drawing.Image.FromStream(memStr);
return image;
}
]]>
</msxsl:script>

The problem I'm hitting is that the process is failing on System.Drawing.Image -- it doesn't know what it is. Here's what I get back:
Server Error in '/' Application.
--------------------------------------------------------------------------------

Script compile errors: file:///c:/inetpub/wwwroot/rss20.xsl(8,1) : error CS0246: The type or namespace name 'Image' could not be found (are you missing a using directive or an assembly reference?)
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Xml.Xsl.XsltException: Script compile errors: file:///c:/inetpub/wwwroot/rss20.xsl(8,1) : error CS0246: The type or namespace name 'Image' could not be found (are you missing a using directive or an assembly reference?)
<<<<<<<<<<<<<<

So my question is how do I add an assembly reference to System.Drawing, and where? This is an ASP.NET page with an XML/XSL transform. I don't have a whole lot of .NET knowledge -- I only JUST discovered I can actually even use C# in XSL (I just tried it out of pure curiosity -- and that gave me some hope!)

Anyone know how to add this assembly reference and where? Thanx!
 
G

Greg Collins [MVP]

Sorry, one modification to the return value of the code, and a slightly different error:

<msxsl:script language="C#" implements-prefix="cs">
<![CDATA[
System.Drawing.Image decode(string s)
{
byte[] image = Convert.FromBase64String( s );
System.IO.MemoryStream memStr = new System.IO.MemoryStream();
memStr.Write( image, 0, image.Length );
System.Drawing.Image img = System.Drawing.Image.FromStream(memStr);
return image;
}
]]>
</msxsl:script>


Error:
Script compile errors: file:///c:/inetpub/wwwroot/rss20.xsl(8,8) : error CS0234: The type or namespace name 'Drawing' does not exist in the class or namespace 'System' (are you missing an assembly reference?)


I've also tried adding to the .aspx page, with no luck, the following:
<%@ import namespace="System.Drawing" %>

I know System.Drawing is there, because if I purposely misspell it to "System.Drawingfoo" then I get an error on this import line.

I've also tried using an import with System.Drawing.Image, with no luck.

--
Greg Collins [InfoPath MVP]
Please visit: http://www.InfoPathDev.com






"Greg Collins [MVP]" <Greg.Collins_AT_InfoPathDev.com> wrote in message I have an ASP.NET page that uses a tag:

<asp:Xml id="foo" runat="server" DocumentSource="rss.xml" TransformSource="rss20.xsl" />

This creates a Web page from an XML file that was generated by InfoPath. By default InfoPath inline's their images in base64 encoding. These need to be extracted out to be displayed.

I've got my XSL set up to be able to pull out images and manage them separately. According to Microsoft, they need to be decoded and returned back as an image stream to the src attribute of the img tag.

See: http://www.mcse.ms/archive180-2004-4-605843.html
See: http://blogs.msdn.com/infopath/archive/2004/04/21/117600.aspx

So I've got some C# code in my XSL that performs this decode operation -- well, that's where I need help. Here's the code:

<msxsl:script language="C#" implements-prefix="cs">
<![CDATA[
Image decode(string s)
{
byte[] image = Convert.FromBase64String( s );
System.IO.MemoryStream memStr = new System.IO.MemoryStream();
memStr.Write( image, 0, image.Length );
System.Drawing.Image img = System.Drawing.Image.FromStream(memStr);
return image;
}
]]>
</msxsl:script>

The problem I'm hitting is that the process is failing on System.Drawing.Image -- it doesn't know what it is. Here's what I get back:
Server Error in '/' Application.
--------------------------------------------------------------------------------

Script compile errors: file:///c:/inetpub/wwwroot/rss20.xsl(8,1) : error CS0246: The type or namespace name 'Image' could not be found (are you missing a using directive or an assembly reference?)
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Xml.Xsl.XsltException: Script compile errors: file:///c:/inetpub/wwwroot/rss20.xsl(8,1) : error CS0246: The type or namespace name 'Image' could not be found (are you missing a using directive or an assembly reference?)
<<<<<<<<<<<<<<

So my question is how do I add an assembly reference to System.Drawing, and where? This is an ASP.NET page with an XML/XSL transform. I don't have a whole lot of .NET knowledge -- I only JUST discovered I can actually even use C# in XSL (I just tried it out of pure curiosity -- and that gave me some hope!)

Anyone know how to add this assembly reference and where? Thanx!
 
P

PL

I had the exact same problem trying to do sql queries inside a msxsl:script block.

I posted a question about that a while ago and got no helpful replies at all.

The only thing I could deduct is that it's actually a documentation error,
that is you cannot use ANY assembly inside a script block, only the
listed default ones.

One way to solve I guess is to skip the inline xmlns:script blocks and
attach an object to the transform instead, there are some articles about
how to do that.

Then you just write your own class and attach it, then call whatever methods
you need from inside the xls.

Let me know if you find another solution, seems this should work accroding to docs.

PL.

"Greg Collins [MVP]" <Greg.Collins_AT_InfoPathDev.com> wrote in message Sorry, one modification to the return value of the code, and a slightly different error:

[snip]
 
G

Greg Collins [MVP]

What were the steps you used to make this work? I'm still fairly new to ASP.NET, C# and the .NET Framework, so specifics, or even a walkthrough, would be helpful.

By the way, another change to the code quoted earlier is that I would be returning "img" and not "image". (Sorry -- I was doing a lot of playing around with the code earlier, and pasted it in to the posting before cleaning it back up, so I missed a few things) :eek:)

--
Greg Collins [InfoPath MVP]
Please visit: http://www.InfoPathDev.com




I had the exact same problem trying to do sql queries inside a msxsl:script block.

I posted a question about that a while ago and got no helpful replies at all.

The only thing I could deduct is that it's actually a documentation error,
that is you cannot use ANY assembly inside a script block, only the
listed default ones.

One way to solve I guess is to skip the inline xmlns:script blocks and
attach an object to the transform instead, there are some articles about
how to do that.

Then you just write your own class and attach it, then call whatever methods
you need from inside the xls.

Let me know if you find another solution, seems this should work accroding to docs.

PL.

"Greg Collins [MVP]" <Greg.Collins_AT_InfoPathDev.com> wrote in message Sorry, one modification to the return value of the code, and a slightly different error:

[snip]
 
P

PL

What were the steps you used to make this work? I'm still fairly new to ASP.NET,
C# and the .NET Framework, so specifics, or even a walkthrough, would be helpful.

Actually the samples are pretty few on this subject as well but you use the AddExtensionObject
and AddParam in the XsltArgumentList class.

These articles have some .NET examples in them (look somewhere at the bottom of both):
http://msdn.microsoft.com/msdnmag/issues/02/03/xml/
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnxml/html/enhancingxsl.asp

Here's a sample from the first MS article, in this sample they created a class called Person
with the public method SayHello and then add it to the transform with AddExtensionObject

The XSL:

<xsl:transform version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:eek:bj="urn:person"<xsl:eek:utput method="text"/>
<xsl:template match="/">
name: <xsl:value-of select="obj:get-name()"/>
age: <xsl:value-of select="obj:get-age()"/>
greeting: <xsl:value-of select="obj:SayHello('Michi')"/>
</xsl:template>
</xsl:transform>

The VB.NET code

XslTransform xslt = new XslTransform();
xslt.Load(stylesheet);
XPathDocument doc = new XPathDocument(filename);
XsltArgumentList xslArg = new XsltArgumentList();

// Create a Person object
Person obj = new Person();
obj.name = "Michi";
obj.age = 6;

// Add it to the transform
xslArg.AddExtensionObject("urn:person", obj);
xslt.Transform(doc, xslArg, Console.Out);

Hope this helps.
PL.
 
A

Anton Lapounov [MSFT]

Hi Greg,

PL's answer is correct, you'd better use extension objects. I am responsible
to fix this problem in Framework 2.0. If you have Beta1, you may try the
following:

<msxsl:script language="..." implements-prefix="...">
<msxsl:assembly name="myassembly.dll"/>
...
</msxsl:script>

Thanks,
Anton

This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use. © 2004 Microsoft Corporation. All rights
reserved.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top