PoshCode Archive  Artifact [03bf7dcfef]

Artifact 03bf7dcfef3afd5e3bce4957ac7b34f95e19e2864a3c59adf7179877def56e84:

  • File ConvertFrom-CliXml.ps1 — part of check-in [14f48d81bb] at 2018-06-10 13:08:24 on branch trunk — A pair with ConvertTo-CliXml — this version closes and disposes the string reader handle. (user: David Sjstrand size: 1581)

# 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()
    }
}