How to parse a XML file and get the Node value using Powershell

 

Let us say that we have sample XMl file like this

<?xml version="1.0" encoding="utf-8"?>
<note>
  <to>ToAll</to>
  <from>FromME</from>
  <heading>Reminder</heading>
  <body>Hello World!</body>
</note>

The following will read the file and get us the value of the nodes

$path= "C:\Sample.xml"
$oXMLDocument=New-Object System.XML.XMLDocument 
$oXMLDocument.Load($path) 
write-host "Value to To:" $oXMLDocument.note.to
write-host "Value to From:"$oXMLDocument.note.from
write-host "Value to Heading:"$oXMLDocument.note.heading
write-host "Value to Body:"$oXMLDocument.note.body

This is easy but when i had a do this and checked how it took me lot of time to come to this.

So thought of sharing

Hope it will be helpful

Sudheesh Narayanaswamy

==========================================================

This posting /Script is provided "AS IS" with no warranties and confers no rights