Reputation: 12154
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"
Upvotes: 1
Views: 1761
Reputation: 96702
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.
Upvotes: 3
Reputation: 10190
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();
Upvotes: 1
Reputation: 5298
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();
Upvotes: 1
Reputation: 5515
Have your looked at this article? It talks about passing params to xslt in C#. I believe it is possible to do.
Upvotes: 1