- <#
- .SYNOPSIS
- This script uses XMLWriter to write an XML Element
- .DESCRIPTION
- This script creates and uses and XML writer to write an
- XML Element
- .NOTES
- File Name : Write-XxmElementString.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2010/05/write-xxmelementstringps1.html
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/aex0e7zs.aspx
- .EXAMPLE
- PSH [C:\foo]: .\Write-XmlElementString.ps1'
- <orderID>1-456-ab</orderID><orderID>2-36-00a</orderID>
- #>
- # Create new XMLWriter settings
- $settings = new-object System.Xml.XmlWriterSettings
- $settings.OmitXmlDeclaration = $true
- $settings.ConformanceLevel = 'Fragment'
- $settings.CloseOutput = $false;
- # Create the XmlWriter object and write some content.
- #$strm = new-object system.io.MemoryStream
- $writer = [system.xml.XmlWriter]::Create("c:\foo\data.xml", $settings)
- $writer.WriteElementString("orderID", "1-456-ab")
- $writer.WriteElementString("orderID", "2-36-00a")
- $writer.Flush()
- $writer.Close()
- # Display data
- cat c:\foo\data.xml
- # End Script
This blog contains PowerShell scripts, more PowerShell scripts and still more PowerShell scripts. Occasionally you may see some organisational posts.
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
Write-XML.ps1
- <#
- .SYNOPSIS
- This script uses XMLWriter to write XML
- .DESCRIPTION
- This script creates and XML writer and writes some basic
- XML.
- .NOTES
- File Name : Write-XML.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2010/05/write-xmlps1.html
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/system.xml.xmlwritersettings.aspx
- .EXAMPLE
- PSH [C:\foo]: .\Write-XML.ps1'
- <?xml version="1.0" encoding="IBM437"?>
- <order
- orderID="367A54"
- date="2001-05-03">
- <price>19.95</price>
- </order>PSH [C:\foo]:
- #>
- # Create a new writer settings object
- # And set settings
- $settings = New-Object system.Xml.XmlWriterSettings
- $settings.Indent = $true
- $settings.OmitXmlDeclaration = $false
- $settings.NewLineOnAttributes = $true
- # Create a new Writer
- $writer = [system.xml.XmlWriter]::Create([system.console]::Out, $settings)
- #Write some XML
- $writer.WriteStartElement("order")
- $writer.WriteAttributeString("orderID", "367A54")
- $writer.WriteAttributeString("date", "2001-05-03")
- $writer.WriteElementString("price", "19.95")
- $writer.WriteEndElement()
- # Flush the writer (and close the file)
- $writer.Flush()
- $writer.Close()
Subscribe to:
Posts (Atom)