# encoding: ascii
# api: powershell
# title: ConvertTo-CliXml
# description: Export-CliXml and Import-CliXml only work with files. This is an implementation for sending CliXML directly to the pipeline. Requires PowerShell 2.0
# version: 0.1
# type: function
# author: David Sjstrand
# license: CC0
# function: ConvertTo-CliXml
# x-poshcode-id: 2301
# x-derived-from-id: 4050
# x-archived: 2016-06-09T05:22:34
# x-published: 2011-10-14T10:50:00
#
#
#requires -version 2.0
function ConvertTo-CliXml {
    param(
        [Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)]
        [ValidateNotNullOrEmpty()]
        [PSObject[]]$InputObject
    )
    begin {
        $type = [type]::gettype("System.Management.Automation.Serializer")
        $ctor = $type.getconstructor("instance,nonpublic", $null, @([System.Xml.XmlWriter]), $null)
        $sw = new-object System.IO.StringWriter
        $xw = new-object System.Xml.XmlTextWriter $sw
        $serializer = $ctor.invoke($xw)
        $method = $type.getmethod("Serialize", "nonpublic,instance", $null, [type[]]@([object]), $null)
        $done = $type.getmethod("Done", [System.Reflection.BindingFlags]"nonpublic,instance")
    }
    process {
        try {
            [void]$method.invoke($serializer, $InputObject)
        } catch {
            write-warning "Could not serialize $($InputObject.gettype()): $_"
        }
    }
    end {    
        [void]$done.invoke($serializer, @())
        $sw.ToString()
		$xw.Close()
		$sw.Dispose()
    }
}
