Reading XML with PowerShell

    Reading XML with PowerShell

    We can read XML files using PowerShell. For example, we have the following content in foods.xml file

    <foods>
       <food>
           <name>Belgian Waffles</name>
           <price>$5.95</price>
           <description>Two of our famous Belgian Waffles with plenty of real maple syrup</description>
           <calories>650</calories>
       </food>

       <food>
         <name>Strawberry Belgian Waffles</name>
         <price>$7.95</price>
         <description>Light Belgian waffles covered with strawberries and whipped cream</description>
         <calories>900</calories>
       </food>

      <food>
         <name>Berry-Berry Belgian Waffles</name>
         <price>$8.95</price>
         <description>Light Belgian waffles covered with an assortment of fresh berries and whipped cream</description>
         <calories>900</calories>
       </food>

      <food>
         <name>French Toast</name>
         <price>$4.50</price>
         <description>Thick slices made from our homemade sourdough bread</description>
         <calories>600</calories>
       </food>

      <food>
         <name>Homestyle Breakfast</name>
         <price>$6.95</price>
         <description>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>
         <calories>950</calories>
       </food>
    </foods>

    Read the xml document

    PS C:\Users\Administrator\Desktop> [xml]$XmlDocument = Get-Content .\foods.xml
    PS C:\Users\Administrator\Desktop> $XmlDocument
     
    foods
    -----
    foods
     
    PS C:\Users\Administrator\Desktop> $XmlDocument.foods
     
    food
    ----
    {Belgian Waffles, Strawberry Belgian Waffles, Berry-Berry Belgian Waffles, French Toast...}
     
    PS C:\Users\Administrator\Desktop> $XmlDocument.foods.food
     
    name                          price                         description                   calories
    ----                          -----                         -----------                   --------
    Belgian Waffles               $5.95                         Two of our famous Belgian ... 650
    Strawberry Belgian Waffles    $7.95                         Light Belgian waffles cove... 900
    Berry-Berry Belgian Waffles   $8.95                         Light Belgian waffles cove... 900
    French Toast                  $4.50                         Thick slices made from our... 600
    Homestyle Breakfast           $6.95                         Two eggs, bacon or sausage... 950
     
    PS C:\Users\Administrator\Desktop> $XmlDocument.foods.food.name
    Belgian Waffles
    Strawberry Belgian Waffles
    Berry-Berry Belgian Waffles
    French Toast
    Homestyle Breakfast

    We can also use to directly read the values using Select-Xml cmdlet.

    PS C:\Users\Administrator\Desktop> $path = "foods.xml"
    PS C:\Users\Administrator\Desktop> $xpath = "/foods/food"
    PS C:\Users\Administrator\Desktop> Select-Xml -Path $path -XPath $xpath | Select-Object -ExpandProperty Node
     
    name                          price                         description                   calories
    ----                          -----                         -----------                   --------
    Belgian Waffles               $5.95                         Two of our famous Belgian ... 650
    Strawberry Belgian Waffles    $7.95                         Light Belgian waffles cove... 900
    Berry-Berry Belgian Waffles   $8.95                         Light Belgian waffles cove... 900
    French Toast                  $4.50                         Thick slices made from our... 600
    Homestyle Breakfast           $6.95                         Two eggs, bacon or sausage... 950

     


    © 2019 WinAdmin.org . All Rights Reserved.
    Cookies make it easier for us to provide you with our services. With the usage of our services you permit us to use cookies.
    Ok Decline