title: PowerShell XML preserve indentation
body: <p>I'm new to PowerShell and tried to write a script that edits a xml-file by simply adding and removing (that part is still missing) depending on whether the line is or isn't already present.
  Yet I've accomplished to successfully add the line in the right position. But upon saving the xml after adding the line the indent somehow is broken/transformed into 2 whitespaces.
  $xml.PreserveWhiteSpace = $true fully breaks the formatting by deleting all the line breaks.
  I also tried XmlWriter which seems to be crashing in background blocking the file for further editing until next reboot.</p>
  
  <p>Here is my original xml-content:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;Configuration xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
      &lt;Application&gt;
          &lt;LastUsedFile&gt;
              &lt;Path&gt;&lt;/Path&gt;
              &lt;CredProtMode&gt;Obf&lt;/CredProtMode&gt;
              &lt;CredSaveMode&gt;NoSave&lt;/CredSaveMode&gt;
          &lt;/LastUsedFile&gt;
      &lt;/Application&gt;
  &lt;/Configuration&gt;
  </code></pre>
  
  <p>That's how it should look like (new element LanguageFile before LastUsedFile):</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;Configuration xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
      &lt;Application&gt;
          &lt;LanguageFile&gt;German.lngx&lt;/LanguageFile&gt;
          &lt;LastUsedFile&gt;
              &lt;Path&gt;&lt;/Path&gt;
              &lt;CredProtMode&gt;Obf&lt;/CredProtMode&gt;
              &lt;CredSaveMode&gt;NoSave&lt;/CredSaveMode&gt;
          &lt;/LastUsedFile&gt;
      &lt;/Application&gt;
  &lt;/Configuration&gt;
  </code></pre>
  
  <p>This is what I'm actually receiving upon saving:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;Configuration xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
    &lt;Application&gt;
      &lt;LanguageFile&gt;German.lngx&lt;/LanguageFile&gt;
      &lt;LastUsedFile&gt;
        &lt;Path&gt;&lt;/Path&gt;
        &lt;CredProtMode&gt;Obf&lt;/CredProtMode&gt;
        &lt;CredSaveMode&gt;NoSave&lt;/CredSaveMode&gt;
      &lt;/LastUsedFile&gt;
  &lt;/Configuration&gt;
  </code></pre>
  
  <p>Buy using that code:</p>
  
  <pre><code>$path = "$env:APPDATA\App"
  $file = "$env:APPDATA\App\App.config.xml"
  $NodeExists = $null
  
  if ([System.IO.Directory]::Exists($path))
      {
          Write-Output "$path existiert."
          if ([System.IO.File]::Exists($file))
              {
                  Write-Output "$file existiert."
                  $xml = [XML](Get-Content $file)
                  $application = $xml.Configuration.Application
                  $NodeExists = $xml.Configuration.Application.LanguageFile
                  Write-Output $NodeExists
                  if(!$NodeExists)
                      {
                          $langelem = $xml.CreateElement('LanguageFile')
                          $langtext = $xml.CreateTextNode('German.lngx')
                          $langelem.AppendChild($langtext)
                          $application.InsertBefore($langelem, $application.FirstChild)
                          Write-Output "Sprache auf Deutsch gestellt."
                      }
                  else
                      {
                          Write-Output "Sprache ist auf Deutsch gesetzt."
                          $application.RemoveChild($application.FirstChild)
                          Write-Output "Sprache auf Englisch gestellt."                       
                      }
                  $xml.Save($file)
              }
          else
              {
                  Write-Output "$file existiert NICHT."
              }
      }
  else
      {
          Write-Output "$path existiert NICHT."
      }
  </code></pre>