XSL Transformation in C# by Jochen Hofmann

Contributed by
Rating: 4 stars4 stars4 stars4 stars4 stars / 6
November 17, 2001
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

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());
}

}
}

blog comments powered by Disqus
ASP.NET ARTICLES

- Implementing ASP.NET 4.0 Page.MetaDescriptio...
- ASP.Net Development Tips
- Intro to Sessions in ASP.Net
- Google Maps API Introduction in ASP.NET usin...
- Creating an ASP.NET 3.5 Gridview Image Galle...
- Encrypt QueryString in ASP.NET 3.5 using VB....
- ASP.NET 3.5 Drop Down List Controls
- Connect to Access Database with ASP.Net
- Secure Audio Streaming with ASP.Net and Flash
- Dynamic Sitemap and Navigation in ASP.Net
- Implement Gzip and Deflate Compression in AS...
- Run ASP.Net in Ubuntu with Apache
- ASP.Net Mono Website Contact Forms
- ASP.Net URL Rewriting Methods
- Murach`s ASP.NET 4 Web Programming with C# 2...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 11 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials