title: Optimising XDocument to XDocument XSLT
body: <p>The following code works but is messy and slow. I am transforming an XDocument to another XDocument using XSLT2 with Saxon, adapted using SaxonWrapper:</p>
  
  <pre><code>public static XDocument HSRTransform(XDocument source)
  {
      System.Reflection.Assembly thisExe = System.Reflection.Assembly.GetExecutingAssembly();
      System.IO.Stream xslfile = thisExe.GetManifestResourceStream("C2KDataTransform.Resources.hsr.xsl");
  
      XmlDocument xslDoc = new XmlDocument();
      xslDoc.Load(xslfile);
  
      XmlDocument sourceDoc = new XmlDocument();
      sourceDoc.Load(source.CreateReader());
  
      var sw = new StringWriter();
  
      Xsl2Processor processor = new Xsl2Processor();
      processor.Load(xslDoc);
  
      processor.Transform(sourceDoc, new XmlTextWriter(sw));
  
      XDocument outputDoc = XDocument.Parse(sw.ToString());
      return outputDoc;
  }
  </code></pre>
  
  <p>I realise that the slowness might actually be in the bits I have no control over but are there better ways to do all the switching between XDocument and XmlDocument and usage of writers?</p>