title: How to loop through XML files and create a CSV file?
body: <p>I've got a good working PowerShell script (thanks to Ansgar Wiechers) for an XML file, that exports me the desired fields to a CSV file:</p>
  
  <pre class="lang-bsh prettyprint-override"><code>$goal = '\\LC\ARCHIV\INPUT_' + (Get-Date -Format yyyyMMddss) + '.xml'
  [xml]$xml = Get-Content '\\mcsonlines-impexp\Onlines\LCMS\IMPORT\*.xml'
  $xml.SelectNodes('//COMPOUND') |
    Select-Object @{n='SampleID';e={[int]$_.ParentNode.id}},
                  @{n='SampleName';e={"B" + $_.ParentNode.name}},
                  @{n='CompoundID';e={[int]$_.id}},
                  @{n='CompoundName';e={$_.name}},
                  @{n='analconc';e={[double]$_.PEAK.analconc}} |
    Export-Csv '\\LC\IMPORT\quandata.csv' -NoType -Delimiter ';'
  
  Move-Item -Path \\LC\IMPORT\*.xml -destination $goal
  </code></pre>
  
  <p>The XML file:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0"?&gt;
  &lt;QUANDATASET&gt;
    &lt;XMLFILE&gt;
    &lt;DATASET&gt;
    &lt;GROUPDATA&gt;
      &lt;GROUP&gt;
        &lt;METHODDATA/&gt;
        &lt;SAMPLELISTDATA&gt;
          &lt;SAMPLE id="1" groupid="1" name="Routine_2016_05_30_002"&gt;
            &lt;COMPOUND id="1" sampleid="1" groupid="1" name="Leu"&gt;
              &lt;PEAK foundscan="0" analconc="0.023423456"&gt;
                &lt;ISPEAK/&gt;
              &lt;/PEAK&gt;
            &lt;/COMPOUND&gt;
            &lt;COMPOUND id="2" sampleid="1" groupid="1" name="Iso"&gt;
               &lt;PEAK foundscan="0" analconc="0.123456789"&gt;
                 &lt;ISPEAK/&gt;
               &lt;/PEAK&gt;
            &lt;/COMPOUND&gt;
            &lt;COMPOUND id="3" sampleid="1" groupid="1" name="Thre"&gt;
            ...
            ...
            ...
          &lt;SAMPLE id="2" groupid="1" name="Routine_2016_05_30_003"&gt;
            &lt;COMPOUND id="1" sampleid="2" groupid="1" name="Leu"&gt;
            ...
            ...
            ...
  </code></pre>
  
  <p>The CSV Export looks like:</p>
  
  <pre>SampleID   SampleName  CompoundID  CompoundName    analconc
  ...
  6   Routine_2016_11_11_006  1   Leu 60,30064828
  6   Routine_2016_11_11_006  2   Iso 60,38823887
  6   Routine_2016_11_11_006  3   Thre 74,00187964
  ...</pre>
  
  <p>Now to my question - is it possible to process multiple XML files at once with the script to a CSV file? With my change makes the script unfortunately nothing at all.</p>
  
  <p>First try:</p>
  
  <pre class="lang-bsh prettyprint-override"><code>$file = Get-ChildItem '\\LC\IMPORT\*.xml' -Recurse
  foreach ($file in $files) {
    [xml]$xml = (Get-Content $file)
    $xml.SelectNodes('//COMPOUND') |
      Select-Object @{n='SampleID';e={[int]$_.ParentNode.id}},
                    @{n='SampleName';e={"B" + $_.ParentNode.name}},
                    @{n='CompoundID';e={[int]$_.id}},
                    @{n='CompoundName';e={$_.name}},
                    @{n='analconc';e={[double]$_.PEAK.analconc}} |
      Export-Csv '\\LC\IMPORT\quandata.csv' -NoType -Delimiter ';'
  }
  </code></pre>
  
  <p>This did not work at all.</p>
  
  <p>Second try:</p>
  
  <pre class="lang-bsh prettyprint-override"><code>Get-ChildItem '\\LC\IMPORT\' *.xml -Recurse | % {
    $xml = [xml](Get-Content $_.FullName)
    #$goal = '\\LC\ARCHIV\INPUT_' + (Get-Date -Format yyyyMMddss) + '.xml'
  
    $xml.SelectNodes('//COMPOUND') |
      Select-Object @{n='SampleID';e={[int]$_.ParentNode.id}},
                    @{n='SampleName';e={"B" + $_.ParentNode.name}},
                    @{n='CompoundID';e={[int]$_.id}},
                    @{n='CompoundName';e={$_.name}},
                    @{n='analconc';e={[double]$_.PEAK.analconc}} |
      Export-Csv '\\LC\IMPORT\quandata.csv' -NoType -Delimiter ';'
  }
  </code></pre>
  
  <p>With this attempt only one XML file is exported to a CSV file.</p>
  
  <p>Here is the link to my first post:</p>
  
  <p><a href="https://stackoverflow.com/questions/40151076/how-to-output-child-elements-separately-not-as-one-space-delimited-string">How to output child elements separately, not as one space-delimited string?</a></p>