title: How to map XML to dynamic properties on an object in PowerShell?
body: <p>I need to automate the addition of many .NET objects to a business system. A PowerShell script needs to read an XML input file and perform the appropriate changes via the business system's API.</p>
  
  <p>The problem I'm finding is that <strong>the objects are of many different types and therefore have different properties</strong>.</p>
  
  <p>Here's an example of the XML:</p>
  
  <pre><code>&lt;BusinessObject&gt;
      &lt;Action&gt;Add&lt;/Action&gt;
      &lt;Id&gt;{867B6C43-2A20-485D-A3E3-CBFCD50CA6F3}&lt;/Id&gt;
      &lt;AssemblyName&gt;ABC.BusinessObjects, Version=1.0.0.0, Culture=neutral, PublicKeyToken=361ad75badc53918&lt;/AssemblyName&gt;
      &lt;ClassName&gt;ABC.BusinessObjects.HealthService&lt;/ClassName&gt;
      &lt;!-- Properties specific to object --&gt;
      &lt;HealthServiceName&gt;Jo's GP Super Center&lt;/HealthServiceName&gt;
  &lt;/BusinessObject&gt;
  &lt;BusinessObject&gt;
      &lt;Action&gt;Add&lt;/Action&gt;
      &lt;Id&gt;{867B6C43-2A20-485D-A3E3-CBFCD50CA6F3}&lt;/Id&gt;
      &lt;AssemblyName&gt;ABC.BusinessObjects, Version=1.0.0.0, Culture=neutral, PublicKeyToken=361ad75badc53918&lt;/AssemblyName&gt;
      &lt;ClassName&gt;ABC.BusinessObjects.Patient&lt;/ClassName&gt;
      &lt;!-- Properties specific to object --&gt;
      &lt;PatientName&gt;Anna Smith&lt;/PatientName&gt;
  &lt;/BusinessObject&gt;
  </code></pre>
  
  <p>Relevant portion of script:</p>
  
  <pre><code>$xmlItem.BusinessObjects.GetElementsByTagName("BusinessObject") | % {
      $businessObject = $_
      if ($businessObject.Action -eq "Add") {
          $assemblyName = $businessObject.AssemblyName
          $className = $businessObject.ClassName
          $assembly = [Reflection.Assembly]::Load($assemblyName)
          $obj = $assembly.CreateInstance($className)
          ### TODO: How to set properties on $obj ???
          $api.AddBusinessObject($obj)
      }
  }
  </code></pre>
  
  <p>I probably need to put the object-specific properties into their own XML element so that I can loop through them. What I'm not sure about is what to do inside that loop.</p>
  
  <p>Assuming each property's XML element name matches the $obj property name, how do I dynamically access and set the property from its corresponding XML value?</p>