15

I have one string inputXMLString, and the second one containg transform XSL named transformXSLString. Both of them are correctly formatted xml's.

How to perform the transformation with XslCompiledTransform in C# so as to get the result also as a string, let's say resultXMLString?

1 Answer 1

27

You can use XmlReader and StringReader respectively StringWriter:

string inputXML = "...";
string transformXSL = "...";

XslCompiledTransform proc = new XslCompiledTransform();

using (StringReader sr = new StringReader(transformXSL))
{
  using (XmlReader xr = XmlReader.Create(sr))
  {
    proc.Load(xr);
  }
}

string resultXML;

using (StringReader sr = new StringReader(inputXML))
{
  using (XmlReader xr = XmlReader.Create(sr))
  {
    using (StringWriter sw = new StringWriter())
    {
      proc.Transform(xr, null, sw);
      resultXML = sw.ToString();
    }
  }
}
1
  • Thanks! Seems a bit nilly willy to have to create a stringreader then an xmlreader then stringwriter ... but this worked for me.
    – enforge
    Apr 23, 2015 at 19:44

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.