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! | Visit IBM developerWorks to download a free trial version of Lotus Quickr 8.0, which enables collaboration by transforming the way everyday business content such as documents, rich media, photos, and video can be shared. Lotus Quickr makes it faster and easier to share content of all types (not just documents) within virtual teams. It is designed to make it easier to collaborate across organizational boundaries, while continuing to work within the context of familiar desktop applications. FREE! Go There Now!
| | | | Discover how IBM Rational AppScan Standard Edition can help you detext vulnerabilities in your web applications in the Web Application Security eKit. IBM Rational AppScan is a leading suite of automated web application security solutions that scan and test for common Web application vulnerabilities. The new Web Application Security eKit provides you with valuable resources, including white papers, demos, and additional information on the benefits of testing your Web applications. FREE! Go There Now!
| | | | Visit IBM developerWorks to download a free trial version of IBM Rational Business Developer V7.1. Rational Business Developer offers rapid and simplified development of business applications and services through Enterprise Generation Language (EGL) tools, generating Java or mainframe solutions while shielding developers from technical complexities. FREE! Go There Now!
| | | | Learn from the best! Find out how developers use Rational ClearCase to be more flexible, innovative and deliver higher quality code in the Rational ClearCase Power Users eKit. This complimentary eKit provides a collection of materials, like articles, whitepapers, and demos that can help you become a power user of Rational ClearCase. FREE! Go There Now!
| | | | This Fall, IBM Rational talks to you directly through a special teleconference series giving you access to the best minds in IBM Rational - product experts and market thought leaders who will answer your questions during these pre-scheduled telephone conference calls. Register today! FREE! Go There Now!
| | | | You can now evaluate IBM Rational Asset Manager V7.0 online without installing or configuring it on your own system! Rational Asset Manager helps create, modify, govern, find, and reuse any type of development assets, including SOA and systems development assets. Rational Asset Manager helps you reduce software development costs and improve quality by facilitating the reuse of all types of software development-related assets. Visit developerWorks to learn more about this product and register to explore its capabilities online. FREE! Go There Now!
| | | | Attend this launch webcast with Scott Hebner, Vice President of IBM Rational Marketing and Strategy, where he will overview Rational’s new offerings and programs to help customers accelerate software innovation on System z. He will discuss how these solutions help organizations extend their core business processes toward modern architectures such as SOA and web technologies to deliver business improvements that stand the test of time. FREE! Go There Now!
| | | | In this webcast, you'll get an introduction to the eXtreme Transaction Processing (XTP) features of WebSphere Extended Deployment and the common architectural traits required by XTP applications. See how WebSphere Extended Deployment's ObjectGrid feature provides a state-of-the-art infrastructure for hosting XTP applications. FREE! Go There Now!
| | | | Viper 2 brings a great value to developer communities including SQL, XML, PHP, Ruby, .NET and Java. You probably already know that DB2 Express-C is free for developers to develop, deploy and distribute. Viper 2 provides a variety of means that help move your application from the development stage to deployment more rapidly. This webcast shows how to best utilize the latest tools available for developing DB2 applications. FREE! Go There Now!
| | | | The unprecedented scope of a service-oriented architecture (SOA) initiative brings to the forefront a number of management and governance issues that were sidestepped in the past. The key to a successful SOA implementation is managing and governing activities throughout the entire SOA delivery lifecycle by ensuring that services conform to the needs of all of the business’s stakeholders. Learn how service lifecycle management allows the business to ensure that the process by which services are defined, created, tested, deployed, optimized and retired is manageable, repeatable and auditable. FREE! Go There Now!
| | | | All FREE IBM® developerWorks Tools! | |