PoshCode Archive  Artifact [863ee0aaca]

Artifact 863ee0aaca362d3c44d2b70cdf6af92d86b57ac54f9f7a2cfd563dd90b90a9c0:

  • File ConvertFrom-CliXml.ps1 — part of check-in [8aaab5a669] at 2018-06-10 13:08:16 on branch trunk — I wrote this based on ConvertTo-CliXml. This takes a xml string from the pipeline and converts it to objects. (user: David Sjstrand size: 1479)

# encoding: ascii
# api: powershell
# title: ConvertFrom-CliXml
# description: I wrote this based on ConvertTo-CliXml. This takes a xml string from the pipeline and converts it to objects.
# version: 0.1
# type: function
# author: David Sjstrand
# license: CC0
# function: ConvertFrom-CliXml
# x-poshcode-id: 2294
# x-archived: 2014-01-03T12:11:21
# x-published: 2011-10-10T14:57:00
#
#
function ConvertFrom-CliXml {
    param(
        [parameter(position=0,mandatory=$true,valuefrompipeline=$true)]
        [validatenotnull()]
        [string]$string
    )
    begin
    {
        $inputstring = ""
    }
    process
    {
        $inputstring += $string
    }
    end
    {
        $type = [type]::gettype("System.Management.Automation.Deserializer")
        $ctor = $type.getconstructor("instance,nonpublic", $null, @([xml.xmlreader]), $null)
        $sr = new-object io.stringreader $inputstring
        $xr = new-object xml.xmltextreader $sr
        $deserializer = $ctor.invoke($xr)
        $method = @($type.getmethods("nonpublic,instance") | where-object {$_.name -like "Deserialize"})[1]
        $done = $type.getmethod("Done", [reflection.bindingflags]"nonpublic,instance")
        while (!$done.invoke($deserializer, @()))
        {
            try {
                $method.invoke($deserializer, "")
            } catch {
                write-warning "Could not deserialize $string: $_"
            }
        }
    }
}