Skip to content

Instantly share code, notes, and snippets.

@ninmonkey
Created September 19, 2024 23:31
Show Gist options
  • Select an option

  • Save ninmonkey/7c3fe2498b78e75ebd8753daa54ae174 to your computer and use it in GitHub Desktop.

Select an option

Save ninmonkey/7c3fe2498b78e75ebd8753daa54ae174 to your computer and use it in GitHub Desktop.
[xml]$doc = gc (Join-Path $PSScriptRoot './sample.xml')
[xml]$Doc = @'
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<setting name = "logLevel" value = "verbose" />
<setting name = "root" value = "c:/foo" desc = "users root dir" />
</configuration>
'@
$script:ErrorActionPreference = 'Continue'
$settings = $doc.SelectNodes('/configuration/setting')
$settings | ft -auto
$allAttrNames = $settings.Attributes.Name | Sort-object -Unique
$summary = $settings | %{
[System.Xml.XmlElement] $elem = $_
$record = @{}
$allAttrNames | %{
$record[ $elem ] = ''
}
$allAttrNames | %{
$name = $_
$value = $elem.Attributes[ $name ].Value ?? ''
$record[ $name ] = $value
# $value = $elem.GetAttribute( $name ) ?? 'fallback'
}
[pscustomobject]$record
}
$summary | Ft -auto
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment