title: Surgical XML editing with Powershell
body: <p>I'm working with csproj files using Powershell to perform large-scale editing of project references. So far I've managed to edit the Include attributes on ProjectReferences using the following lines:</p>
  
  <pre><code>    $projectXml = [xml](Get-Content $_.Project.FullName)
      Add-TfsPendingChange -edit $_.Project.FullName -ErrorAction Stop
      $projectXml | Select-Xml -namespace @{msb = "http://schemas.microsoft.com/developer/msbuild/2003"} -xpath "//msb:ProjectReference[msb:Project='$projectGuid']" | Select-Object -ExpandProperty Node | foreach { $_.Include = "$newPath" }
      $projectXml.Save($_.Project.FullName)
  </code></pre>
  
  <p>This works, and replaces the Include attribute on the appropriate ProjectReferences as I expect. However, there are plenty of additional "harmless" changes made, such as formatting all tags on their own line, e.g.
  <br/><code>&lt;FileUpgradeFlags&gt;&lt;/FileUpgradeFlags&gt;</code></p>
  
  <p>becomes</p>
  
  <p><code>&lt;FileUpgradeFlags&gt;</code><br/>
  <code>&lt;/FileUpgradeFlags&gt;</code></p>
  
  <p>Is there any way to perform an edit such as this that doesn't have these side effects?</p>
  
  <p>edit: for clarity to anyone who finds this post for other reasons, Select-MsBuildXml is just a wrapper function I wrote around Select-Xml that pre-loads the namespace parameter with the msbuild namespace, and expands the node property afterwards.</p>