Search notes:

System.Xml.XmlWriter (class)

Using a stream to write a file

The following example uses a stream (System.IO.Stream) into which an XML document is written:
set-strictMode -version latest

[System.IO.Stream]             $stream   = [System.IO.File]::Create("$pwd\test.xml")
[System.Xml.XmlWriterSettings] $settings = new-object System.Xml.XmlWriterSettings

$settings.Indent        = $true
$settings.IndentChars   = '   '

$xmlWriter = [System.Xml.XmlWriter]::Create($stream, $settings);

$xmlWriter.WriteStartDocument()
$xmlWriter.WriteComment("Created at $(get-date -UFormat '%F' )")

$xmlWriter.WriteStartElement('rt')
$XmlWriter.WriteAttributeString('attr', 'val')

for ($id=1; $id -le 3; $id++) {

    $xmlWriter.WriteStartElement('elem')
    $xmlWriter.WriteAttributeString('id', $id)

       $xmlWriter.WriteElementString('foo', -join ( [byte][char]'a' .. [byte][char]'z' | foreach-object {[char] $_} | get-random -count (get-random -minimum 4 -maximum 10) ) )
       $xmlWriter.WriteElementString('bar', $id -eq 2)
       $xmlWriter.WriteElementString('baz', 'x' * $id )

    $xmlWriter.WriteEndElement()
}

$xmlWriter.WriteEndElement()
$xmlWriter.WriteEndDocument()

$xmlWriter.Flush()
$xmlWriter.Close()
$stream.Close()
Github repository .NET-API, path: /System/Xml/XmlWriter/stream.ps1

See also

System.Xml.XmlReader

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/Microsof...', 1759399360, '216.73.216.42', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/Microsoft/dot-net/namespaces-classes/System/Xml/XmlWriter/index(74): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78