private/ConvertFrom-XML.ps1
<#
.SYNOPSIS Converts XML elements into PowerShell objects. .DESCRIPTION This function takes XML elements as input and converts them into PowerShell objects. It recursively processes the XML elements and their properties, converting them into appropriate data types. .PARAMETER InputObject Specifies the XML elements to be converted. .EXAMPLE $xml = @" <Person> <Name>John</Name> <Age>30</Age> </Person> "@ $xmlObject = [xml]$xml $personObject = $xmlObject.Person | ConvertFrom-XML .NOTES Function : ConvertFrom-XML Author : John Billekens Copyright: Copyright (c) AppVentiX Version : 1.0 #> Function ConvertFrom-XML { [cmdletbinding()] Param ( [Parameter(Mandatory, ValueFromPipeLine)] [Xml.XmlElement[]]$InputObject ) Begin { $output = @() } Process { Foreach ($element in $InputObject) { $properties = $element | Get-Member -MemberType Property $object = @{} Foreach ($property in $properties) { $valueName = $property.Name Switch -Wildcard ($property.Definition) { 'string*' { $object."$valueName" = ConvertTo-DataType -String $element.$valueName } 'System.Xml.XmlElement*' { $value = $element.$valueName | ConvertFrom-XML $object."$valueName" = $value } 'System.Object*' { $value = @() Foreach ($entry in $element.$valueName) { $value += $entry | ConvertFrom-XML } $object."$valueName" = $value } } } } $output += [PSCustomObject]$object } End { Return $output } } |