How do I use XslCompiledTransform when input XML and transform XSL are strings. How do I get the transformation result as a string?
Asked Answered
M

1

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?

Murr answered 6/5, 2013 at 8:43 Comment(0)
F
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();
    }
  }
}
Forego answered 6/5, 2013 at 8:55 Comment(1)
Thanks! Seems a bit nilly willy to have to create a stringreader then an xmlreader then stringwriter ... but this worked for me.Intervenient

© 2022 - 2024 — McMap. All rights reserved.