Performing Server Side Transforms

As I mentioned in the note at the start of the previous section, client-side automatic transforms are useful only if all your clients use IE. Future browser versions and other client types will no doubt support more XML/XSLT operations, but right now, the only cross-client solution is to perform the transform on the server and send the XHTML result to the client.

Note Before you continue, comment out or delete the line that defines the stylesheet reference in the fiexibie.xmi file in the chl5 folder of your CSharpASP project. Remember that XML comments are the same as HTML comments (<! — —>). Save the changes, then continue.

There are several ways to perform XSLT transforms in .NET, but the easiest way is to drag an XML Web server control onto your Web Form's design surface. For example, create a new Web Form named chl5-l.aspx in the chl5 folder. Add an XML Server control and click the Custom Property button in the Properties dialog for the DocumentSource property. In the resulting dialog, select the chl5 folder in the Projects pane and then select the fiexibie.xmi file from the Contents pane. You'll see the dialog in Figure 15.3.

Accept the default settings and click OK to close the dialog. Repeat the process to set the Transform-Source property to the fiexibie.xsi file. Now run the Web Form. The result looks identical to performing the client-side transform, but it isn't. You can prove that to yourself by viewing the browser source. This time, rather than the XML file contents, you'll see the HTML resulting from the transform. In other words, the server performed the transform for you and automatically sent the result to the browser.

[■mfmc -jrfl

hp-üivp

J

1 'h iifc -h

-j

. _J

Figure 15.3: The Select XML File dialog

The Xmi Web server control is extremely convenient, but what if you need to process an XML document in some other way, such as extracting only some individual data items, saving all or a selected portion of a document to a database, or performing a transform and then modifying the results using C#? To do that, you need to learn a little more about how XML works in .NET and about the classes available in the System.Xml namespace.

Was this article helpful?

0 0

Post a comment