XSL Transformation in C# by A C# class That can transform an XMLDocument to HTML (Text) or or an XMLDocument. It is very flexible and provides several overloads for the transform method. You can post this class to your C# or XML section. Any comments / improvements to the code are appreciated. Jochen.... |
Here's a sample How to call the class: XSLTransform objTrans = new XSLTransform(); Response.Write(objTrans.Transform("d:\projects\test.xml","d:\test.xsl")); |
Here is the code for the Class using System;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;
using System.IO;
namespace KontoDotNet3
{
///
/// Zusammenfassungsbeschreibung fĂĽr XSLTransform.
///
public class XSLTransform
{
private string strXSLFile;
public string XSLFile
{
set
{
strXSLFile = value;
}
get
{
return strXSLFile;
}
}
public XSLTransform()
{
}
public XSLTransform(string sXSLFile)
{
strXSLFile = sXSLFile;
}
public string Transform(ref XmlDocument xd)
{
XPathNavigator xdNav = xd.CreateNavigator();
XslTransform tr = new XslTransform();
tr.Load(strXSLFile);
StringWriter sw = new StringWriter();
tr.Transform(xdNav,null,sw);
return sw.ToString();
}
public string Transform(ref XmlDocument xd, ref XmlDocument xsl)
{
XPathNavigator xdNav = xd.CreateNavigator();
XslTransform tr = new XslTransform();
tr.Load(new XmlNodeReader(xsl));
//Bug in DotNet Beta 2
normal: tr.Load(xsl);
StringWriter sw = new StringWriter();
tr.Transform(xdNav,null,sw);
return sw.ToString();
}
public string Transform(string sXMLDocument, string sXSLFile)
{
XmlDocument xd = new XmlDocument();
xd.Load(sXMLDocument);
XPathNavigator xdNav = xd.CreateNavigator();
XslTransform tr = new XslTransform();
tr.Load(sXSLFile);
StringWriter sw = new StringWriter();
tr.Transform(xdNav,null,sw);
return sw.ToString();
}
public string Transform(string sXMLDocument)
{
XmlDocument xd = new XmlDocument();
xd.Load(sXMLDocument);
XPathNavigator xdNav = xd.CreateNavigator();
XslTransform tr = new XslTransform();
tr.Load(strXSLFile);
StringWriter sw = new StringWriter();
tr.Transform(xdNav,null,sw);
return sw.ToString();
}
public void Transform(ref XmlDocument xd, ref XmlDocument xsl, _
ref XmlDocument retXD)
{
XPathNavigator xdNav = xd.CreateNavigator();
XslTransform tr = new XslTransform();
tr.Load(new XmlNodeReader(xsl));
//Bug in DotNet Beta 2 normal: tr.Load(xsl);
StringWriter sw = new StringWriter();
tr.Transform(xdNav,null,sw);
retXD.LoadXml(sw.ToString());
}
}
}
| | DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More ASP.NET Articles More By aspfree developerWorks - FREE Tools! | Join this webcast, to learn how the Rational Process Library can help with compliance issues, drive process improvement, and assist in service-oriented architecture (SOA) or Agile development. We will take a peek into the Rational Process Library with content around software and systems engineering (including RUP), operations and systems management, program and portfolio management, and asset and SOA governance. FREE! Go There Now!
| | | | Join this Rational Talks to You teleconference, featuring Paul Boustany and Mark Krasovich, to speak to the experts about becoming a Rational ClearCase power user. Get a chance to ask your questions and learn tips and tricks for using Rational ClearCase in Agile development FREE! Go There Now!
| | | | Attend this launch webcast with Scott Hebner, Vice President of IBM Rational Marketing and Strategy, for an overview of Rational’s new software offerings and resources to help modernize and accelerate software innovation on i on Power Systems – while ensuring past application investments are protected and continue to grow. Learn how these solutions are helping customers extend their core i5/OS solutions toward modern architectures such as SOA and web technologies to deliver business improvements that stand the test of time. FREE! Go There Now!
| | | | Achieving true agility is a never-ending effort. We will showcase how you can become agile incrementally, a few practices at the time.Which practices should any agile team strive to adopt? What additional practices should you consider based on your needs to scale? Adopting practices are however made much easier with the right tool support. What about if your tools adapt to your practices? We will take a look at how the Jazz technology can be leveraged to make your process change the behavior of your tools. FREE! Go There Now!
| | | | Learn to enable users to both rate existing animations and to combine existing animations into new snippets. This is the third in a series of three tutorials that chronicle the building of a site that enables collaborative discussion and animation building using Domino and OpenLaszlo. FREE! Go There Now!
| | | | Download the Rational Application Developer (RAD) v7.5 open beta code and start developing applications for the JEE5 standard which features EJB3.0, JPA, JSF 1.2, JSP 2.1 and Servlet 2.5 standards. When you use this beta you will see how you can increase developer productivity for already existing applications with improved support for refactoring, as well as adding new features to existing applications. In addition, the beta provides tooling for JD Edwards, Oracle, SAP, Siebel and PeopleSoft to improve the developer productivity with these enterprise systems. FREE! Go There Now!
| | | | Listen to this webcast to get an overview of Info 2.0 and a technical demo of how to quickly build an enterprise mashup. IBM's Info 2.0 technology leverages emerging Web 2.0 technologies such as mashups, feeds, AJAX, and JSON in order to simplify assembly of information using feeds and services. Come learn about the technical elements of Info 2.0 including the Feed Generation framework, Mashup Engine, and mashup assembly components. Learn how to pull information from databases, departmental information, and the Web to create mashups critical to your company’s success. We will also discuss best practices to help you get started. FREE! Go There Now!
| | | | Get a free trial download of IBM Lotus Forms V3.0 (formerly Workplace Forms), which provides a zero-footprint eForms solution to help you automate and move forms-based business processes off the desktop and onto the Web. With Lotus Forms, you can extend applications beyond the firewall by creating a single electronic form document ready for use in both thick and Web 2.0 thin client format. FREE! Go There Now!
| | | | Get a free trial download of the latest version of IBM Rational Performance Tester V7.0.1, a load and performance testing solution for teams concerned about the scalability of their Web-based applications. Combining multiple ease-of-use features with granular detail, Rational Performance Tester simplifies the test-creation, load-generation and data-collection processes that help teams ensure the ability of their applications to accommodate required user loads. FREE! Go There Now!
| | | | Get a free trial download of the latest version of IBM Rational Tester for SOA Quality V7.0.1, a functional and regression testing tool that enables the creation, comprehension, modification and execution of testing GUI-less Web services. FREE! Go There Now!
| | | | All FREE IBM® developerWorks Tools! | |