In other words, there is a faster and more concise way to write the following code:
//Create an object for performing XSTL transformations XslCompiledTransform xslt = new XslCompiledTransform(); xslt.Load(HttpContext.Current.Server.MapPath("/xslt/" + xsltfile.Value), new XsltSettings(true, false), new XmlUrlResolver()); //Create a XmlReader object to read the XML we want to format //XmlReader needs an input stream (StringReader) StringReader sr = new StringReader(node.OuterXml); XmlReader xr = XmlReader.Create(sr); //Create a StringWriter object to capture the output from the XslCompiledTransform object StringWriter sw = new StringWriter(); //Perform the transformation xslt.Transform(xr, null, sw); //Retrieve the transformed XML from the StringWriter object string transformedXml = sw.ToString();
UPDATE (thanks for all the answers so far!):
Sorry for my uncertainty: “faster” and more “succinctly” I mean, do I include any unnecessary steps? In addition, I would like a more “readable” solution if anyone has it. I use this code in a small part of the web application that I am developing, and I am going to port it to most of the application, so I want to make sure that it is as neat as it can be before I make a move.
In addition, I get XML from a static class (in a separate library of data access classes) that communicates with the database. I also manipulate the converted XML string before posting it to a web page. I am not sure if the I / O streams are still viable in this case.
One more thing: the supplied XML and XSLT can change (application users can make changes to both), so I think I would have to compile it every time.
source share