PoshCode Archive  Artifact [5a7aab1a8c]

Artifact 5a7aab1a8c3e4318670ad9eb38ea1c5cceaa04274498fe6b6013467781998605:

  • File ConvertFrom-CliXml.ps1 — part of check-in [eb1a0a62eb] at 2018-06-10 13:42:50 on branch trunk — A pair with ConvertTo-CliXml — this version closes and disposes the string reader handle. Now works in PowerShell 3 and 4 as well. (user: Poshoholic size: 1681)

# encoding: ascii
# api: powershell
# title: ConvertFrom-CliXml
# description: A pair with ConvertTo-CliXml — this version closes and disposes the string reader handle. Now works in PowerShell 3 and 4 as well.
# version: 0.1
# type: function
# author: Poshoholic
# license: CC0
# function: ConvertFrom-CliXml
# x-poshcode-id: 4545
# x-derived-from-id: 5743
# x-archived: 2016-03-05T17:49:15
# x-published: 2016-10-23T01:13:00
#
#
#requires -version 2.0
function ConvertFrom-CliXml {
    param(
        [Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)]
        [ValidateNotNullOrEmpty()]
        [String[]]$InputObject
    )
    begin
    {
        $OFS = "`n"
        [String]$xmlString = ""
    }
    process
    {
        $xmlString += $InputObject
    }
    end
    {
        $type = [PSObject].Assembly.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)
        $done = $type.GetMethod('Done', [System.Reflection.BindingFlags]'nonpublic,instance')
        while (!$type.InvokeMember("Done", "InvokeMethod,NonPublic,Instance", $null, $deserializer, @()))
        {
            try {
                $type.InvokeMember("Deserialize", "InvokeMethod,NonPublic,Instance", $null, $deserializer, @())
            } catch {
                Write-Warning "Could not deserialize ${string}: $_"
            }
        }
        $xr.Close()
        $sr.Dispose()
    }
}