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:\> [xml]$XmlDocument = Get-Content .\foods.xml
PS C:\> $XmlDocument

foods
-----
foods


PS C:\> $XmlDocument.foods

food
----
{Belgian Waffles, Strawberry Belgian Waffles, Berry-Berry Belgian Waffles, French Toast...}

PS C:\> $XmlDocument.foods.food

name                        price description                                                                         calories
----                        ----- -----------                                                                         --------
Belgian Waffles             $5.95 Two of our famous Belgian Waffles with plenty of real maple syrup                   650
Strawberry Belgian Waffles  $7.95 Light Belgian waffles covered with strawberries and whipped cream                   900
Berry-Berry Belgian Waffles $8.95 Light Belgian waffles covered with an assortment of fresh berries and whipped cream 900
French Toast                $4.50 Thick slices made from our homemade sourdough bread                                 600
Homestyle Breakfast         $6.95 Two eggs, bacon or sausage, toast, and our ever-popular hash browns                 950

PS C:\> $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:\> $path = "foods.xml"
PS C:\> $xpath = "/foods/food"
PS C:\> Select-Xml -Path $path -XPath $xpath | Select-Object -ExpandProperty Node

name                        price description                                                                         calories
----                        ----- -----------                                                                         --------
Belgian Waffles             $5.95 Two of our famous Belgian Waffles with plenty of real maple syrup                   650
Strawberry Belgian Waffles  $7.95 Light Belgian waffles covered with strawberries and whipped cream                   900
Berry-Berry Belgian Waffles $8.95 Light Belgian waffles covered with an assortment of fresh berries and whipped cream 900
French Toast                $4.50 Thick slices made from our homemade sourdough bread                                 600
Homestyle Breakfast         $6.95 Two eggs, bacon or sausage, toast, and our ever-popular hash browns                 950