title: Updating xml elements using powershell
body: <p>I have an XSL file, which acts as a configuration file for my application. In fact it is an XML file, which has the <code>&lt;xsl:Stylesheet&gt;</code> elements wrapped around it. This file is called  <code>Config.xsl</code>:</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"           xmlns="http://www.example.org/Config"&gt;
       &lt;xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"               standalone="yes" /&gt;
       &lt;xsl:template match="/"&gt;
       &lt;Config&gt;
            &lt;Test&gt;somevalue&lt;/Test&gt;
            &lt;Test1&gt;someothervalue&lt;/Test1&gt;
       &lt;/Config&gt;
       &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>Now I would like to change the value of an element, which is passed dynamically.
  Meaning, I have another XML file, which contains the XPATH and the value as key name/value pairs. Below is the contents of XML file <code>Properties.xml</code>.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;ConfigFiles&gt;
  &lt;ConfigFile&gt;
      &lt;FileName&gt;Config.xsl&lt;/FileName&gt;
      &lt;Keys&gt;
          &lt;Key Name="Config.Test" Value="newvalue" /&gt;
          &lt;Key Name="Config.Test1" Value="newvalue1" /&gt;
      &lt;/Keys&gt;
  &lt;/ConfigFile&gt;   
  &lt;/ConfigFiles&gt;
  </code></pre>
  
  <p>Below is my powershell code, which is not updating the values of the elements.</p>
  
  <pre><code>$properties = [xml] (Get-Content Properties.xml)
  $lstfiles = $properties.ConfigFiles.ConfigFile
  foreach($file in $lstfiles)
  {
    $configfilename = $file.FileName
    $filename = "C:\configs\configfilename"
    $testconfigfile = [xml] (Get-Content $filename)
  
    $lstKeys = $file.Keys.Key
    foreach($key in $lstKeys)
    {
      #When I debug the code, I was able to assign the value using the below code (Commented). However this is not dynamic
      #$testconfigfile.DocumentElement.LastChild.Config.Test = "newvalue"
  
      #Now if I try to pass the same values dynamically by reading them from properties.xml and assigning it using the below code it does not work
      $testconfigfile.DocumentElement.LastChild.$key.Name = $key.Value            
    }
    $testconfigfile.Save($filename)               
  }
  </code></pre>