title: Transform XML in Powershell /.Net save result to a variable (type object) NOT a file
body: <p>Hi All I an trying to transform xml stored as an <code>XmlDocument</code> object type using <code>System.Xml.Xsl.XslCompiledTransform</code> </p>
  
  <p>I am trying to do this in Powershell.
  The input xml is in the form of an xml object type</p>
  
  <p>I have the actual transform stored as an external file.</p>
  
  <p>I want the transformed xml to be held in another xmlDocument type object and not written out to a file.</p>
  
  <p>When I look up the docs on the <code>System.Xml.Xsl.XslCompiledTransform</code> class, 
  there are several overloads of the transform method</p>
  
  <p>Each method variation wants to write the resultant transform to either a <code>stream</code> OR <code>XmlWriter</code> OR other file writing device, however I don't want to write the result to a file, I want to an <code>XmlDocument</code> object type to insert into another xml file.</p>
  
  <p>Reference is here: <a href="https://msdn.microsoft.com/en-us/library/system.xml.xsl.xslcompiledtransform(v=vs.110).aspx" rel="nofollow noreferrer" title="Class reference MSDN">Reference Docs MSDN XslCompiledTransform Class</a></p>
  
  <p>Here is my xml as a </p>
  
  <pre><code>$mydoc = [xml] @"
  &lt;metadata&gt;
      &lt;LOGLINE&gt;Teena's First Horrific Slumber Party&lt;/LOGLINE&gt;
      &lt;YearVideoMade&gt;1987&lt;/YearVideoMade&gt;
      &lt;LevelOfStudy&gt;Masters&lt;/LevelOfStudy&gt;
      &lt;SYNOPSIS&gt;Teena will never forget her first slumber party,blah&lt;/SYNOPSIS&gt;
      &lt;Director&gt;Shirley Barrett&lt;/Director&gt;
      &lt;Writer&gt;Shirley Barrett&lt;/Writer&gt;
      &lt;Producer&gt;Edmund Milts/Shirley Barrett&lt;/Producer&gt;
      &lt;Cinematographer&gt;Joanne Parker&lt;/Cinematographer&gt;
      &lt;Editor&gt;Kym Vaitiekus&lt;/Editor&gt;
      &lt;ProductionDesigner&gt;Diana Reynolds&lt;/ProductionDesigner&gt;
      &lt;LocationSound&gt;Kate Gunn&lt;/LocationSound&gt;
      &lt;PostSound&gt;Kate Gunn&lt;/PostSound&gt;
      &lt;AreaOfSpecialisation&gt;Scriptwriting&lt;/AreaOfSpecialisation&gt;
      &lt;AFTRSSTUDENTKEYCREATIVECREW&gt;Shirley Barrett: director&lt;/AFTRSSTUDENTKEYCREATIVECREW&gt;
      &lt;AFTRSSTUDENTKEYCREATIVECREW&gt;Shirley Barrett: writer&lt;/AFTRSSTUDENTKEYCREATIVECREW&gt;
      &lt;AFTRSSTUDENTKEYCREATIVECREW&gt;Edmund Milts/Shirley Barrett: producer&lt;/AFTRSSTUDENTKEYCREATIVECREW&gt;
      &lt;AFTRSSTUDENTKEYCREATIVECREW&gt;Joanne Parker: cinematographer&lt;/AFTRSSTUDENTKEYCREATIVECREW&gt;
      &lt;AFTRSSTUDENTKEYCREATIVECREW&gt;Kym Vaitiekus: editor&lt;/AFTRSSTUDENTKEYCREATIVECREW&gt;
      &lt;AFTRSSTUDENTKEYCREATIVECREW&gt;Diana Reynolds: production designer&lt;/AFTRSSTUDENTKEYCREATIVECREW&gt;
      &lt;AFTRSSTUDENTKEYCREATIVECREW&gt;Kate Gunn: location sound&lt;/AFTRSSTUDENTKEYCREATIVECREW&gt;
      &lt;AFTRSSTUDENTKEYCREATIVECREW&gt;Kate Gunn: post sound&lt;/AFTRSSTUDENTKEYCREATIVECREW&gt;
      &lt;DirectorID&gt;000000&lt;/DirectorID&gt;
      &lt;WriterID&gt;000000&lt;/WriterID&gt;
      &lt;ProducerID&gt;000000&lt;/ProducerID&gt;
      &lt;ProducerID&gt;000000&lt;/ProducerID&gt;
      &lt;CinematographerID&gt;000000000000&lt;/CinematographerID&gt;
      &lt;EditorID&gt;000000&lt;/EditorID&gt;
      &lt;ProductionDesignerID&gt;000000&lt;/ProductionDesignerID&gt;
      &lt;LocationSoundID&gt;000000&lt;/LocationSoundID&gt;
      &lt;PostSoundID&gt;000000&lt;/PostSoundID&gt;
  &lt;/metadata&gt;
  "@
  </code></pre>
  
  <p>Here is my transform</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
  &lt;xsl:output method="xml" omit-xml-declaration="yes"/&gt;
  
   &lt;xsl:template match="metadata"&gt;
      &lt;xsl:copy&gt;
      &lt;xsl:copy-of select="LOGLINE"/&gt;
      &lt;xsl:copy-of select="YearVideoMade"/&gt;
      &lt;xsl:copy-of select="LevelOfStudy"/&gt;
      &lt;xsl:copy-of select="SYNOPSIS"/&gt;
      &lt;xsl:copy-of select="Director"/&gt;
      &lt;xsl:copy-of select="DirectorID"/&gt;
      &lt;xsl:copy-of select="Writer"/&gt;
      &lt;xsl:copy-of select="WriterID"/&gt;
      &lt;xsl:copy-of select="Producer"/&gt;
      &lt;xsl:copy-of select="ProducerID"/&gt;
      &lt;xsl:copy-of select="Cinematographer"/&gt;
      &lt;xsl:copy-of select="CinematographerID"/&gt;
      &lt;xsl:copy-of select="Editor"/&gt;
      &lt;xsl:copy-of select="EditorID"/&gt;
      &lt;xsl:copy-of select="ProductionDesigner"/&gt;
      &lt;xsl:copy-of select="ProductionDesignerID"/&gt;
      &lt;xsl:copy-of select="LocationSound"/&gt;
      &lt;xsl:copy-of select="LocationSoundID"/&gt;
      &lt;xsl:copy-of select="PostSound"/&gt;
      &lt;xsl:copy-of select="PostSoundID"/&gt;
      &lt;xsl:copy-of select="Composer"/&gt;
      &lt;xsl:copy-of select="ComposerID"/&gt;
      &lt;xsl:copy-of select="Screenmusic"/&gt;
      &lt;xsl:copy-of select="ScreenMusicID"/&gt;
      &lt;xsl:copy-of select="FESTIVALSANDAWARDS"/&gt;
      &lt;xsl:copy-of select="AreaOfSpecialisation"/&gt;
      &lt;xsl:copy-of select="RestrictedVideo"/&gt;
      &lt;xsl:copy-of select="RestrictedVideoText"/&gt;
      &lt;xsl:copy-of select="AFTRSSTUDENTKEYCREATIVECREW"/&gt;
      &lt;/xsl:copy&gt;
   &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>PS.. If I do write the transformed content to a file, then the transform works as expected so I know the stylesheet does what is asked of it  </p>
  
  <p>Regards</p>
  
  <p>Angus</p>
  
  <p><strong>Additional information in light of Mathais R Jensen's response:</strong></p>
  
  <p>The XML to be transformed is obtained from a web service call and extracted from the returned XML package. The XML extract is html encoded, so I decode it with 
  <code>System.Web.HttpUtility.decode()</code> then cast the resultant string to XML-->XMl Node --> XPathDocument --> XPathNavigator</p>
  
  <pre><code> [xml] $metaDstr = [System.Web.HttpUtility]::HtmlDecode($metaD)
   $nodeReader = New-Object System.Xml.XmlNodeReader($metaDStr)
   $XPD = New-Object System.Xml.XPath.XPathDocument($nodeReader)
   $XPreader = $XPD.CreateNavigator()
  </code></pre>
  
  <p>At this point I have been inserting <code>$XPreader</code> into a node of another document generated by the <code>XmlTextWriter</code> class using the <code>XmlTextWriter.WriteNode()</code> method</p>
  
  <p>however I need to transform the extracted XML to get the nodes in the correct order for ingestion into another system.</p>
  
  <p>If I call the transform method on the <code>$XPreader</code> I get the error message</p>
  
  <pre><code>Exception calling "Transform" with "2" argument(s): "The specified path, file name, or 
  both are too long. The fully qualified file name must be less than 260 characters, and 
  the directory name must be less than 248 characters."   
  </code></pre>
  
  <p>The transform is looking for a file input.</p>