Creating documents

PowerShell can be used to create XML documents from scratch. One possible way to do this is by using the XmlWriter class:

$writer = [System.Xml.XmlWriter]::Create("$pwd
ewfile.xml") 
$writer.WriteStartDocument() 
$writer.WriteStartElement('cars') 
$writer.WriteStartElement('car') 
$writer.WriteAttributeString('type', 'Saloon') 
$writer.WriteElementString('colour', 'Green') 
$writer.WriteEndElement() 
$writer.WriteEndElement() 
$writer.Flush() 
$writer.Close() 

Elements opened by WriteStartElement must be closed to maintain a consistent document.

The XmlWriter class is a buffered writer. The Flush method is called at the end to push the content of the buffer back to the file.

The format of generated XML can be changed by supplying an XmlWriterSettings object when calling the Create method. For example, it might be desirable to write line breaks and indent elements, as shown in the following example:

$writerSettings = New-Object System.Xml.XmlWriterSettings 
$writerSettings.Indent = $true 
$writer = [System.Xml.XmlWriter]::Create( 
    "$pwd
ewfile.xml", 
    $writerSettings 
) 
$writer.WriteStartDocument() 
$writer.WriteStartElement('cars') 
$writer.WriteStartElement('car') 
$writer.WriteAttributeString('type', 'Saloon') 
$writer.WriteElementString('colour', 'Green') 
$writer.WriteEndElement() 
$writer.WriteEndElement() 
$writer.Flush() 
$writer.Close() 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.201.71