Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
function ConvertFrom-Xml {
<#
.SYNOPSIS
Converts XML object to PSObject representation for further ConvertTo-Json transformation
.EXAMPLE
# JSON->XML
$xml = ConvertTo-Xml (get-content 1.json | ConvertFrom-Json) -Depth 4 -NoTypeInformation -as String
.EXAMPLE
# XML->JSON
ConvertFrom-Xml ([xml]($xml)).Objects.Object | ConvertTo-Json
#>
param([System.Xml.XmlElement]$Object)
if (($Object -ne $null) -and ($Object.Property -ne $null)) {
$PSObject = New-Object PSObject
foreach ($Property in @($Object.Property)) {
if ($Property.Property.Name -like 'Property') {
$PSObject | Add-Member NoteProperty $Property.Name ($Property.Property | % {ConvertFrom-Xml $_})
} else {
if ($Property.'#text' -ne $null) {
$PSObject | Add-Member NoteProperty $Property.Name $Property.'#text'
} else {
if ($Property.Name -ne $null) {
$PSObject | Add-Member NoteProperty $Property.Name (ConvertFrom-Xml $Property)
}
}
}
}
$PSObject
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment