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"

1

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
1

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

2 Comments

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 ..
This page also has an example that's easy to follow: msdn.microsoft.com/en-us/library/…
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();

2 Comments

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

2 Comments

We don't need XDocument anymore here .. Code works fine without that .. passing the parameter list is "typical" I agree with that ..
You're right, I just chopped the code from something that's live and working (-:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.