tags:

views:

240

answers:

4

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"

+2  A: 

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

MandoMando
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 ..
infant programmer
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
Second link is cool .. Thanks
infant programmer
+1  A: 

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
I was about to upvote, but this code is now out of date... it will work, but its not the right classes any more.
Murph
We don't need XDocument anymore here .. Code works fine without that .. passing the parameter list is "typical" I agree with that ..
infant programmer
+1  A: 

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
We don't need XDocument anymore here .. Code works fine without that .. passing the parameter list is "typical" I agree with that ..
infant programmer
You're right, I just chopped the code from something that's live and working (-:
Murph
+2  A: 

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
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
This code is cool .. :) I have implemented similar code finally ..
infant programmer