Tuesday, 19 May 2009

Get-XMLFileContents.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script reads a simple XML file and prints the results 
  4. .DESCRIPTION 
  5.     This script is a re-write of an XML C# MSDN Sample. 
  6. .NOTES 
  7.     File Name  : get-xmlfilecontents.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell V2 CTP3 
  10.     Books3.xml =  
  11.       <book> 
  12.       <title>C# Introduction</title> 
  13.       <price>20</price> 
  14.       </book> 
  15. .LINK 
  16.     This script posted to: 
  17.         http://www.pshscripts.blogspot.com 
  18.     MSDN Sample posted at: 
  19.         http://msdn.microsoft.com/en-us/library/t9bfea29.aspx 
  20. .EXAMPLE 
  21.     PSH [C:\foo]: .\Get-XMLFileContents.PS1 
  22.     The content of the title element: C# Introduction 
  23.     The content of the price element: 20 
  24.   
  25. #> 
  26.   
  27. ## 
  28. # Start of Script 
  29. ## 
  30.   
  31. # Create XML Reader 
  32. $reader = [system.Xml.XmlReader]::Create("C:\foo\book3.xml"
  33.   
  34. # Parse the XML document.  ReadString is used to  
  35. # read the text content of the elements. 
  36. $result=$reader.Read() 
  37.   
  38. # Read/Display title 
  39. $reader.ReadStartElement("book"
  40. $reader.ReadStartElement("title")    
  41. "The content of the title element: {0}" -f $reader.ReadString() 
  42. $reader.ReadEndElement() 
  43.   
  44. # Read and display price 
  45. $reader.ReadStartElement("price"
  46. "The content of the price element: {0}" -f $reader.ReadString() 
  47. $reader.ReadEndElement() 
  48. $reader.ReadEndElement() 
  49. # End of Script 

No comments: