1

This is the question I posted :

Hi, I have an XSLT code which needs to take parameters (say one or two) from C# code .. (If you want to know, why I need to do this, then let me explain, I have to parse an input XML from certain external application, however I need to edit data of some tags taking the values of some other application which could be defined in complex C# code, I don't have to worry about it) .. for the time being and for demo purpose, I need to declare some strings and pass them to XSLT following the action of triggering the transformation.

I tried to search google, but didn't work. If you get to know ANYTHING regarding this, please send me corresponding link or information ..

As I am not familiar with C# (thats the reason stuck with problem) simpler coding would help a lot ..

And also please specify which "project type" should I select ..

thanks in advance ..

And the solution is here: http://msdn.microsoft.com/en-us/library/system.xml.xsl.xsltargumentlist.addparam.aspx simple and works very conveniently ..

thanQ MandoMando and thanQ "stackoverflow"

Community
  • 1
  • 1
Rookie Programmer Aravind
  • 11,952
  • 23
  • 81
  • 114
  • 1
    See also: http://stackoverflow.com/questions/1778299/1778326#1778326 - just add an `XsltArgumentList` with the param values. – Marc Gravell Nov 23 '09 at 18:00

4 Answers4

3

Generally speaking, it's not necessary to create DOM objects like XmlDocument or XDocument to execute transforms.

XslCompiledTransfrom xslt = new XsltCompiledTransform()
xslt.Load(transformPath);
XsltArgumentList args = new XsltArgumentList();
args.AddParam("name", "myNamespace", value)
using (XmlReader xr = XmlReader.Create(inputPath))
using (XmlWriter xw = XmlWriter.Create(outputPath))
{
   xslt.Transform(xr, args, xw);
}

Note that the Create() methods of XmlReader and XmlWriter have a formidable number of overloads. I use XmlWriter.Create(Console.Out) a lot when I'm prototyping.

Robert Rossney
  • 94,622
  • 24
  • 146
  • 218
  • 1
    In some cases (i.e. html output), a StringWriter is better for the output (as it won't be strict xml), but a good answer. – Marc Gravell Nov 23 '09 at 19:02
1

Have your looked at this article? It talks about passing params to xslt in C#. I believe it is possible to do.

MandoMando
  • 5,215
  • 4
  • 28
  • 35
  • ya .. exactly that is where I got the Idea .. but I am looking for some thing ready or kind of "detailed" tutorial .. (Please don't mind .. its a human psychology to go for more lazy things) .. If I don't get any more suggestions I will rollback to the same site to start from scratch.. your advice is valuable .. thnx for the reply .. – Rookie Programmer Aravind Nov 23 '09 at 17:54
  • 1
    This page also has an example that's easy to follow: http://msdn.microsoft.com/en-us/library/system.xml.xsl.xsltargumentlist.addparam.aspx – MandoMando Nov 23 '09 at 18:10
1

Quick and dirty:

XmlDocument x = new XmlDocument();
x.Load("yourxmldoc.xml");
XslTransform t = new XslTransform();
XsltArgumentList xslArg = new XsltArgumentList();
xslArg.AddParam("parameterName", "", parameterValue);
StringWriter swEndDoc = new System.IO.StringWriter();
t.Load("yourdoc.xslt");
t.Transform(x, xslArg, swEndDoc, null);
String output = swEndDoc.ToString();
pixel
  • 5,298
  • 8
  • 35
  • 32
1

This is fairly straightforward - note that I'm using XDocument and XslCompiledTransform:

XDocument xmlDocument = XDocument.Load(fromSource); // Or whatever means to get XML
XsltArgumentList xslArgs = new XsltArgumentList();

// For as many params as you need
xslArgs.AddParam("paramName", "", "paramValue");

// Create and load an XSLT transform - with params matching param names above
XslCompiledTransform t = new XslCompiledTransform();
t.Load(XSLTPath);

StringWriter outputDoc = new System.IO.StringWriter();
t.Transform(xmlDocument.CreateReader(), xslArgs, outputDoc);

String output = outputDoc.ToString();
Murph
  • 9,985
  • 2
  • 26
  • 41