Public/New-OpmlDocument.ps1

function New-OpmlDocument
{
    param
    (
        [string]$Title,
        [DateTime]$DateCreated,
        [DateTime]$DateModified,
        [string]$OwnerName,
        [string]$OwnerEMail,
        [string]$OwnerId,
        [string]$Docs = 'http://opml.org/spec2.opml',
        [int[]]$ExpansionState,
        [int]$VertScrollState,
        [Nullable[int]]$WindowTop = $null,
        [Nullable[int]]$WindowLeft = $null,
        [Nullable[int]]$WindowBottom = $null,
        [Nullable[int]]$WindowRight = $null,
        [Parameter(ValueFromPipeline)][Outline[]]$Outline
    )

    begin
    {
        $CachedItems = New-Object -TypeName System.Collections.Generic.List[Outline]
    }

    process
    {
        if ($Outline) { $CachedItems.AddRange($Outline) }
    }

    end
    {
        $StringWriter = New-Object -TypeName System.IO.StringWriter
        $XmlWriterSettings = New-Object -TypeName System.Xml.XmlWriterSettings
        $XmlWriterSettings.Indent = $true
        $XmlWriter = [System.Xml.XmlWriter]::Create($StringWriter, $XmlWriterSettings)

        $XmlWriter.WriteStartDocument()
        $XmlWriter.WriteStartElement('opml')
        $XmlWriter.WriteAttributeString('version', '2.0')

        $XmlWriter.WriteStartElement('head')
        if ($Title) { $XmlWriter.WriteElementString('title', $Title) }
        if ($DateCreated) { $XmlWriter.WriteElementString('dateCreated', $DateCreated.ToString('ddd, d MMM yyyy HH:mm:ss zzz')) }
        if ($DateModified) { $XmlWriter.WriteElementString('dateModified', $DateModified.ToString('ddd, d MMM yyyy HH:mm:ss zzz')) }
        if ($OwnerName) { $XmlWriter.WriteElementString('ownerName', $OwnerName) }
        if ($OwnerEMail) { $XmlWriter.WriteElementString('ownerEmail', $OwnerEMail) }
        if ($OwnerId) { $XmlWriter.WriteElementString('ownerId', $OwnerId) }
        if ($Docs) { $XmlWriter.WriteElementString('docs', $Docs) }
        if ($ExpansionState) { $XmlWriter.WriteElementString('expansionState', $ExpansionState -join ',') }
        if ($VertScrollState) { $XmlWriter.WriteElementString('vertScrollState', $VertScrollState) }
        if ($PSBoundParameters.ContainsKey('WindowTop')) { $XmlWriter.WriteElementString('windowTop', $WindowTop) }
        if ($PSBoundParameters.ContainsKey('WindowLeft')) { $XmlWriter.WriteElementString('windowLeft', $WindowLeft) }
        if ($PSBoundParameters.ContainsKey('WindowBottom')) { $XmlWriter.WriteElementString('windowBottom', $WindowBottom) }
        if ($PSBoundParameters.ContainsKey('WindowRight')) { $XmlWriter.WriteElementString('windowRight', $WindowRight) }
        $XmlWriter.WriteEndElement(); # head

        $XmlWriter.WriteStartElement('body')
        $CachedItems | ForEach-Object { $_.Write($XmlWriter) }
        $XmlWriter.WriteEndElement() # body

        $XmlWriter.WriteEndElement(); # opml
        $XmlWriter.WriteEndDocument()
        $XmlWriter.Flush()

        $StringWriter.ToString()
    }
}