# encoding: ascii
# api: powershell
# title: ConvertFrom-CliXml
# description: A pair with ConvertTo-CliXml — this version closes and disposes the string reader handle.
# version: 0.1
# type: function
# author: David Sjstrand
# license: CC0
# function: ConvertFrom-CliXml
# x-poshcode-id: 2302
# x-derived-from-id: 4051
# x-archived: 2016-11-07T09:20:38
# x-published: 2010-10-14T10:52:00
#
#

function ConvertFrom-CliXml {
    param(
        [Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)]
        [ValidateNotNullOrEmpty()]
        [String[]]$InputObject
    )
    begin
    {
		$OFS = "`n"
        [String]$xmlString = ""
    }
    process
    {
        $xmlString += $InputObject
    }
    end
    {
        $type = [type]::gettype("System.Management.Automation.Deserializer")
        $ctor = $type.getconstructor("instance,nonpublic", $null, @([xml.xmlreader]), $null)
        $sr = new-object System.IO.StringReader $xmlString
        $xr = new-object System.Xml.XmlTextReader $sr
        $deserializer = $ctor.invoke($xr)
        $method = @($type.getmethods("nonpublic,instance") | where-object {$_.name -like "Deserialize"})[1]
        $done = $type.getmethod("Done", [System.Reflection.BindingFlags]"nonpublic,instance")
        while (!$done.invoke($deserializer, @()))
        {
            try {
                $method.invoke($deserializer, "")
            } catch {
                write-warning "Could not deserialize $string: $_"
            }
        }
		$xr.Close()
		$sr.Dispose()
    }
}
