Friday, 21 May 2010

Write-XML.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script uses XMLWriter to write XML 
  4. .DESCRIPTION 
  5.     This script creates and XML writer and writes some basic 
  6.     XML. 
  7. .NOTES 
  8.     File Name  : Write-XML.ps1 
  9.     Author     : Thomas Lee - tfl@psp.co.uk 
  10.     Requires   : PowerShell Version 2.0 
  11. .LINK 
  12.     This script posted to: 
  13.         http://pshscripts.blogspot.com/2010/05/write-xmlps1.html 
  14.     MSDN Sample posted at: 
  15.        http://msdn.microsoft.com/en-us/library/system.xml.xmlwritersettings.aspx  
  16. .EXAMPLE 
  17.     PSH [C:\foo]: .\Write-XML.ps1' 
  18.     <?xml version="1.0" encoding="IBM437"?> 
  19.     <order 
  20.       orderID="367A54" 
  21.       date="2001-05-03"
  22.       <price>19.95</price> 
  23.     </order>PSH [C:\foo]: 
  24. #> 
  25. # Create a new writer settings object 
  26. # And set settings 
  27. $settings = New-Object system.Xml.XmlWriterSettings 
  28. $settings.Indent = $true 
  29. $settings.OmitXmlDeclaration = $false 
  30. $settings.NewLineOnAttributes = $true 
  31.  
  32. # Create a new Writer 
  33. $writer = [system.xml.XmlWriter]::Create([system.console]::Out, $settings
  34.  
  35. #Write some XML 
  36. $writer.WriteStartElement("order"
  37. $writer.WriteAttributeString("orderID", "367A54"
  38. $writer.WriteAttributeString("date", "2001-05-03"
  39. $writer.WriteElementString("price", "19.95"
  40. $writer.WriteEndElement() 
  41. # Flush the writer (and close the file)     
  42. $writer.Flush() 
  43. $writer.Close() 

No comments: