title: Powershell- Convert Complex XML to CSV
body: <p>I want to convert the below XML to CSV. Though it has header line information in the XML file. </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;myfile&gt;
    &lt;updatetime&gt;2019-07-30 08:30:30&lt;/updatetime&gt;
    &lt;process code="PRS1234" name="PROCESS1234" /&gt;
    &lt;equipment code="EQP1234" name="EQUIPMENT1234" /&gt;
    &lt;product type="equipment" planned="300" time="36000" cycletime="20" /&gt;
    &lt;shift code="1" timestart="2019-07-30 02:00:00"&gt;
      &lt;order index="1" goodproduct="500" defectproduct="5" time="2019-07-30 02:00:00" /&gt;
      &lt;order index="2" goodproduct="980" defectproduct="7" time="2019-07-30 03:00:00" /&gt;
      &lt;order index="3" goodproduct="1200" defectproduct="12" time="2019-07-30 04:00:00" /&gt;
      &lt;order index="4" goodproduct="1800" defectproduct="15" time="2019-07-30 05:00:00" /&gt;
      &lt;order index="5" goodproduct="2500" defectproduct="15" time="2019-07-30 06:00:00" /&gt;
    &lt;shift&gt;
    &lt;shift code="2" timestart="2019-07-30 07:00:00"&gt;
      &lt;order index="1" goodproduct="600" defectproduct="5" time="2019-07-30 07:00:00" /&gt;
      &lt;order index="2" goodproduct="980" defectproduct="7" time="2019-07-30 08:00:00" /&gt;
      &lt;order index="3" goodproduct="1500" defectproduct="8" time="2019-07-30 09:00:00" /&gt;
      &lt;order index="4" goodproduct="1700" defectproduct="11" time="2019-07-30 10:00:00" /&gt;
      &lt;order index="5" goodproduct="3000" defectproduct="15" time="2019-07-30 11:00:00" /&gt;
    &lt;/shift&gt;
  &lt;/myfile&gt;
  </code></pre>
  
  <p>I can get values for the desired nodes. This is what I have done.</p>
  
  <pre><code>[xml]$inputFile = Get-Content "Q:\XML\FileComplex.xml"
  $inputFile.myfile.product | Select-Object -Property type,planned,time,cycletime | ConvertTo-Csv -NoTypeInformation -Delimiter ";" | Set-Content -Path "Q:\XML\FileComplex.csv" -Encoding UTF8 "
  </code></pre>
  
  <p>What I'm trying to achieve is to combine all desired information together and to get it as one record of the CSV file, which is like this</p>
  
  <pre><code>updatetime          | code(process) | name(process) | code(equipment) | name(equipment) | type(product) | planned(product) | time(product) | cycletime(product) | goodproduct(shift(code) is 1 and index is max) | defectproduct(shift(code) is 1 and index is max) | goodproduct(shift(code) is 2 and index is max) | defectproduct((shift(code) is 2 where index is max)
  2019-07-30 08:30:30 | PRS1234       | PROCESS1234   | EQP1234         | EQUIPMENT1234   | equipment     | 300              | 36000         | 20                 |                               2500             |                                               15 |                                           3000 |                                       15
  </code></pre>
  
  <p>I really appreciate your support!!</p>
  
  <p>Thanks in advance
  Natasha</p>