jeudi 10 janvier 2013

Reading xml from PowerShell is really easy !


It is amusing 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:\temp\myCoverageInfo.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 %



--> Share/Bookmark

Aucun commentaire: