Showing posts with label System.XML.XMLWriter. Show all posts
Showing posts with label System.XML.XMLWriter. Show all posts

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 

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()