Created
January 28, 2018 17:29
-
-
Save f-bader/74658d7c85ba35bba37e9e8291daa711 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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