title: PowerShell - create a variable from an existing XML node sibling
body: <p>First off, let me mention that I am a beginner at PowerShell and would like to thank everyone for their help in advance</p>
  
  <p>I have a function in a PowerShell script that creates numerous new XML nodes using </p>
  
  <pre><code>$fileElement = $xml.CreateElement("FileRef")
  $fileElement.SetAttribute("Id",$refId)
  </code></pre>
  
  <p>This works fine so I have several node siblings called FileRef, each with different Id attributes. The $fileElement variable gets used again later on in the script where it gets it's own child nodes added, using $fileElement.AppendChild. </p>
  
  <p>I now have circumstances where I have a loop that calls the script function multiple times passing in the same $refId variable. The problem I have is that the output is getting duplicated FileRef node siblings with the same id. </p>
  
  <p>What I would like to do, under certain circumstances, is create the $fileElement variable from an existing FileRef node sibling with the id = $refId (so that I can still use AppendChild on the variable later on in the script) instead of create a new XML node sibling with the id = $refId (which is causing the duplication). E.g.</p>
  
  <pre><code>if(circumstances) 
  {
      # first call to the function in the loop, so create new node sibling
      $fileElement = $xml.CreateElement("FileRef")
      $fileElement.SetAttribute("Id",$refId)
  }
  else 
  {
      # node sibling already exists, do not create new node,  
      # use existing node sibling with id = $refId
      create xml node variable $fileElement here
  }
  </code></pre>
  
  <p>Thanks</p>