Illustration de l'article

Reading xml from PowerShell is really easy!

Écrit par Luc

It is amazing to see how simple we can read xml node values from powershell:

Here is a peace of xml file

<?xml version="1.0" standalone="yes"?>
<CoverageDSPriv>
  <Module>
    <ModuleName>myLib.dll</ModuleName>
    <ImageSize>122880</ImageSize>
    <ImageLinkTime>0</ImageLinkTime>
    <LinesCovered>791</LinesCovered>
    <LinesPartiallyCovered>23</LinesPartiallyCovered>
    <LinesNotCovered>856</LinesNotCovered>
    <BlocksCovered>1220</BlocksCovered>
    <BlocksNotCovered>1289</BlocksNotCovered>
    <NamespaceTable>
      <BlocksCovered>939</BlocksCovered>
      <BlocksNotCovered>1054</BlocksNotCovered>
    </NamespaceTable>
  </Module>
</CoverageDSPriv>

PowerShell converts XML elements to properties on .NET objects without the need to write any parsing code!

$coverageFileXml = C:tempmyCoverageInfo.xml
[xml]$xml = (get-content $coverageFileXml)
[int]$BlocksCovered = $xml.CoverageDSPriv.Module.BlocksCovered
[int]$BlocksNotCovered = $xml.CoverageDSPriv.Module.BlocksNotCovered
[int]$TotalBlocks = $BlocksCovered + $BlocksNotCovered
$CoveragePercent = $BlocksCovered \* 100 / $TotalBlocks
$CoveragePercent2Decimals = "{0:N2}" -f $CoveragePercent
Write-Host "Coverage is $CoveragePercent2Decimals %"

Output is : Coverage is 48.62 %

More exemples here.

Illustration de l'article
comments powered by Disqus