Friday, 21 May 2010

Write-XmlElementString.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script uses XMLWriter to write an XML Element 
  4. .DESCRIPTION 
  5.     This script creates and uses and XML writer to write an  
  6.     XML Element 
  7. .NOTES 
  8.     File Name  : Write-XxmElementString.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-xxmelementstringps1.html
  14.     MSDN Sample posted at: 
  15.         http://msdn.microsoft.com/en-us/library/aex0e7zs.aspx  
  16. .EXAMPLE 
  17.     PSH [C:\foo]: .\Write-XmlElementString.ps1' 
  18.     <orderID>1-456-ab</orderID><orderID>2-36-00a</orderID> 
  19. #> 
  20. # Create new XMLWriter settings 
  21. $settings = new-object System.Xml.XmlWriterSettings 
  22. $settings.OmitXmlDeclaration = $true 
  23. $settings.ConformanceLevel = 'Fragment' 
  24. $settings.CloseOutput = $false
  25.  
  26. # Create the XmlWriter object and write some content. 
  27. #$strm = new-object system.io.MemoryStream 
  28. $writer = [system.xml.XmlWriter]::Create("c:\foo\data.xml", $settings
  29. $writer.WriteElementString("orderID", "1-456-ab"
  30. $writer.WriteElementString("orderID", "2-36-00a"
  31. $writer.Flush() 
  32. $writer.Close() 
  33.  
  34. # Display data 
  35. cat c:\foo\data.xml 
  36. # End Script 

No comments: