What is the most optimized way to convert XSLT to ASP.NET?

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.

+4
source share
5 answers

Here is the code I made for my ASP.NET, which is very similar to yours:

  XDocument xDoc = XDocument.Load("output.xml"); XDocument transformedDoc = new XDocument(); using (XmlWriter writer = transformedDoc.CreateWriter()) { XslCompiledTransform transform = new XslCompiledTransform(); transform.Load(XmlReader.Create(new StreamReader("books.xslt"))); transform.Transform(xDoc.CreateReader(), writer); } // now just output transformedDoc 
+11
source

If you have a large XSLT, you can save the overhead of compiling it at run time by compiling XSLT into a .NET assembly when you create the project (for example, as a post-build step). The compiler for this is called xsltc.exe and is part of Visual Studio 2008.

To download such a pre-compiled XSLT , you will need the .NET Framework 2.0 Service Pack 1 (SP1) or later installed on your server (this feature was introduced with Service Pack 1).

For an example, check out Anton Lapunov's blog article:

XSLTC - Compile XSLT Assembly for .NET

If XSLT pre-compilation is not an option, you should consider caching XslCompiledTransform after loading it so that you do not have to compile it every time you want to perform the conversion.

+7
source

Since you mention ASP.NET, the question is, can you directly use the response stream for your conversion output, and can you directly use the input stream if it's POST ...

+2
source

You do not have time to make a complete example, but some notes:

  • XML is not the same as System.String. Get it from a class library as an XDocument or XmlDocument; end with it as an XDocument or XmlDocument.
  • You can use ASP.NET Cache to store compiled XSL with a cache dependency on modifying the .XSLT file.
  • Do not convert XML to string and then back to XML. Use node.CreateNavigator().ReadSubTree() .
  • Similarly, use XPathNavigator.AppendChild to get the XmlWriter that will be written to the XML document.
+2
source

I would rewrite the code as follows:

 string path = HttpContext.Current.Server.MapPath("/xslt/" + xsltfile.Value); XmlReader reader = CreateXmlReader(node.OuterXml); string transformedXml = Transform(path, reader); private XmlReader CreateXmlReader(string text) { StringReader reader = new StringReader(text); return XmlReader.Create(reader); } private string Transform(string xsltPath, XmlReader source) { XsltCompiledTransform transformer = new XsltCompiledTransform(); transformer.Load( xsltPath, new XsltSettings(true, false), new XmlUrlResolver()); StringWriter writer = new StringWriter(); transformer.Transform(source, null, writer); return writer.ToString(); } 

The reason I rewrote code like this is because each block of code now has one and only one purpose. This makes reading and comprehension easier. In addition, the code requires fewer comments, because a lot of information can be inferred from the names of the function and its parameters.

0
source

All Articles