Sign in to follow this  
capn_midnight

[.net] XML and XSL

Recommended Posts

capn_midnight    1707
I've got a bunch of XML files and a single XSL file that is being applied to them. I need to add the transformed documents to a PDF file, and the only way I have to add pages to the PDF is by rendering my text to System.Drawing.Bitmaps and sending it on through a preexisting framework that I am integrating with. I've figured out the text rendering part (even the necessary manual word wrapping), but I really, really don't want to duplicate all the work done by the XSL by manually parsing the XML file and pulling out the information that I want. I thought I had a brilliant idea at one point. I thought I could use Internet Explorer as a control and then render the control to my Bitmap. Well, I was able to get IE running as a control, but for some reason I can't get any controls to render to the Graphics object that I create from the image. In fact, I can't get any control to render like this. Shouldn't this work:
using System;
using System.Drawing;
using System.Windows.Forms;

namespace A{
    public class MyBrowserControl:AxSHDocVw.AxWebBrowser
	{
		public MyBrowserControl():base()
		{
		}
		public Bitmap Save()
		{
			Bitmap buffer = new Bitmap(this.Width, this.Height);
			Graphics g = Graphics.FromImage(buffer);
			PaintEventArgs args = new PaintEventArgs(g, new Rectangle(0, 0, buffer.Width, buffer.Height));
			this.OnPaint(args);
			return buffer;
		}
	}
}


AxSHDocVw.AxWebBrowser comes from the AxInterop.SHDocVw.dll assembly that is automatically generated when you include IE as a COM control on the designer toolbox in Visual Studio. Of course, this idea presents its own problems. If I get this working I'll have to figure out how to scroll the browser window so I can take snapshots of the transformed document. That's only marginally more appealling to me than traversing the XML file. Any other ideas?

Share this post


Link to post
Share on other sites
gwihlidal    1004
Hmm,

Clever idea, though AxWebBrowser is an ActiveX control and I think because of this it doesn't support drawing the way managed controls do. There is a different painting method, and I think it is just a WM_PAINT message or something like that sent to the control. Maybe see if there are other paint hooks you can tap into.

~Graham

Share this post


Link to post
Share on other sites
CoffeeMug    852
Quote:
Original post by capn_midnight
I really, really don't want to duplicate all the work done by the XSL by manually parsing the XML file and pulling out the information that I want.

Why would you have to duplicate any work? Can't you use Xalan.NET or something?

Share this post


Link to post
Share on other sites
Talonius    643
You're wanting to obtain the final results of the transformation? What does the transformation do to the data?

If the XSL formats the XML files as HTML then you're probably on the only track I can think of at the moment with using Internet Explorer as a control.

If, however, the XSL document simply adds to the data or formats it and the result is pure text that you want to blit to a GDI object - that can be done much simpler than using Internet Explorer as the middle man.


XmlDocument xmlDoc = new XmlDocument(pathToFile);
XslTransform transform = new XslTransform(pathToFile);
StringWriter sw = new StringWriter();
transform.Transform(xmlDoc, null, sw);

// Output of transform is held by sw.


Depending on the size of the XML document being transformed you may want to look into using an XPathDocument or similar. As well, I'm not positive the above code works on .NET 2.0. It was pulled from our system which runs on 1.1.

Share this post


Link to post
Share on other sites
capn_midnight    1707
the transform does some formatting as well as organizing, but the formatting is miniscule enough that I can discard it if necessary.

I tried applying the transform using your code, Talonius, but I'm getting exceptions thrown from the XSLT compiler at runtime. Apparently our transform isn't fully standards compliant. I tried to correct the errors, but this thing is too freaking huge.

[Edited by - capn_midnight on March 15, 2006 12:21:29 PM]

Share this post


Link to post
Share on other sites
igni ferroque    415
Quote:
I need to add the transformed documents to a PDF file, and the only way I have to add pages to the PDF is by rendering my text to System.Drawing.Bitmaps and sending it on through a preexisting framework that I am integrating with.

That sucks!

Have a look at XSL FO. There are links to libraries for .NET on that page.

Share this post


Link to post
Share on other sites
Talonius    643
Quote:
Original post by capn_midnight
I tried applying the transform using your code, Talonius, but I'm getting exceptions thrown from the XSLT compiler at runtime. Apparently our transform isn't fully standards compliant. I tried to correct the errors, but this thing is too freaking huge.


Heh, well, even small XSL can be confusing. :)

What exceptions are being thrown, if it doesn't reveal confidential information? I'd like to help; I've played with XML and XSL quite a bit and I'm still looking for "real life" situations to apply my miniscule knowledge.

Share this post


Link to post
Share on other sites
capn_midnight    1707
Quote:
Original post by Talonius
Quote:
Original post by capn_midnight
I tried applying the transform using your code, Talonius, but I'm getting exceptions thrown from the XSLT compiler at runtime. Apparently our transform isn't fully standards compliant. I tried to correct the errors, but this thing is too freaking huge.


Heh, well, even small XSL can be confusing. :)

What exceptions are being thrown, if it doesn't reveal confidential information? I'd like to help; I've played with XML and XSL quite a bit and I'm still looking for "real life" situations to apply my miniscule knowledge.


don't worry about it. The XSL is over 3000 lines long and the smallest of the XML files is just as large. It wasn't expected to be completed anyway, I just hoped I could succeed where everyone else failed. Oh well, I have plenty of other wunderchild points to ride on already.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this