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! | Hold your calendar on January 30, 2008 for this free webcast on the new i5/OS. Rational's Enterprise Modernization products will be discussed at this webcast as they help to drive the application development environment for this new System i OS. <br />And learn how i5/OS will take you to the next step of efficient, resilient business processing. You will hear about the new i5/OS capabilities as it will be the most significant i5/OS release in years. If you cannot join the webcast on 1/30/08 you can still use this link to listen to the replay.<br /> FREE! Go There Now!
| | | | Visit IBM developerWorks to download a free trial version of WebSphere Business Modeler Advanced V6.1.1, IBM’s premier business process modeling and analysis tool for business users that offers process modeling, simulation, and analysis capabilities. IBM WebSphere Business Modeler helps you visualize, understand, and document business processes for continuous improvement. FREE! Go There Now!
| | | | Download a free trial version of IBM DB2 9.5 for Linux, UNIX, and Windows. DB2 9 is the result of a five-year development project that transformed traditional (static) database technology into an interactive data server that merges the high performance and ease of use of DB2 with the self-describing benefits of XML. FREE! Go There Now!
| | | | Visit IBM developerWorks to download IBM DB2 Express-C 9.5, a no-charge version of DB2 Express 9 database server. DB2 Express-C offers the same core data server base features as other DB2 Express editions and provides a solid base to build and deploy applications developed using C/C++, Java, .NET, PHP, and other programming languages. FREE! Go There Now!
| | | | Download the IBM WebSphere Portal V6.1 beta code and learn more about the rich features and enhancements in IBM WebSphere Portal V6.1. WebSphere Portal provides a composite application or business mashup framework and the advanced tooling needed to build flexible, SOA-based solutions, and scalability to meet the needs of any size organization. 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!
| | | | 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!
| | | | Get a free trial download of the latest version of IBM Rational Functional Tester V7.0.1. Rational Functional Tester is an automated functional and regression testing solution for QA teams concerned with the quality of their Java, Microsoft Visual Studio .NET, and Web-based applications. FREE! Go There Now!
| | | | Get a free trial download of the latest version of IBM Rational Method Composer V7.2 which helps you deliver customized yet consistent process guidance to your project teams and IT organization, and includes the latest version of IBM Rational Unified Process (RUP), which has provided process guidance to teams since 1996. 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!
| | | | All FREE IBM® developerWorks Tools! | |