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

StackOverflow https://stackoverflow.com/questions/16395042

문제

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?

도움이 되었습니까?

해결책

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();
    }
  }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top