title: Saving XML document during NuGet init script not working
body: <p>I am trying to write a NuGet package init.ps1 script that will modify one value of a .targets file (XML) and save the document. The script does everything successfully without error, but when I check the document it has not been changed.</p>
  
  <p>Here is the script:</p>
  
  <pre class="lang-bsh prettyprint-override"><code>Param($installPath, $toolsPath, $package)
  
  $proj = Get-Project
  $pack = $package
  
  # Detect if the project installing the NuGet package is a web application and
  # alters an xml element value in the .targets value to prevent duplication
  # of application resources when published.
  
  if ($proj.ExtenderNames -contains "WebApplication") {
      # Begin to build neccessary path strings to find the .targets file.
      # The targets file is currently located in
      # \packages\packageName.packageVersion\build\packageName.targets.
      $packageName = [string]$pack.Id
  
      $packageRootDir = $installPath
  
      # packageName.Version\build
      $packageBuildFolderPath = Join-Path $packageRootDir "build"
  
      # packageName.Version\build\packageName
      $targetsFilePath = Join-Path $packageBuildFolderPath ($packageName + ".targets")
      "$targetsFilePath"
  
      if (Test-Path $targetsFilePath) {
          # If the targets file path has correctly located the file then
          # we edit the targets file to alter the CopyToOutputDirectory element.
  
          # Load the targets file as an xml object
          $xml = New-Object System.Xml.XmlDocument
          $xml.Load($targetsFilePath)
          "xml loaded"
          # Search each ItemGroup element for the one containing a Content element.
          foreach ($group in $xml.Project.ItemGroup) {
              $nodeExists = $group.Content.CopyToOutputDirectory
  
              if ($nodeExists) {
                  "$nodeExists"
                  # Edits the value when we find the correct node
                  $nodeExists = "Never"
                  "$nodeExists"
              }
          }
          "xml modified"
  
          # Save the updated document to the correct place.
          $savePath = [string]$targetsFilePath
          "$savePath"
          $xml.Save($savePath)
          "xml Saved to $savePath"
      }
  }
  </code></pre>
  
  <p>And here is the Package Manager Output from the beginning to end of the script block:</p>
  
  <pre>
  Executing script file 'path to tools/Init.ps1'
  'correct path to package/build/package.targets'
  xml loaded
  Always
  Never
  xml modified
  'correct path to package/build/package.targets'
  xml Saved to 'correct path to package/build/package.targets'
  </pre>