Skip to content

Instantly share code, notes, and snippets.

@ivanyankulov
Last active March 30, 2022 11:55
Show Gist options
  • Save ivanyankulov/40bcb0968499035b370d34429003694e to your computer and use it in GitHub Desktop.
Save ivanyankulov/40bcb0968499035b370d34429003694e to your computer and use it in GitHub Desktop.
PowerShell XML Browser
## http://sptrenches.com/2017/08/build-treevie-xml-powershell.html
## Example: .\New-XMLBrowser.ps1 -XMLPath "C:\fba.azdev.l80\web.config" -RootElementName configuration
[CmdletBinding()]
param (
[parameter(Mandatory=$true)]
[string]$XMLPath,
[parameter(Mandatory=$true)]
[string]$RootElementName
)
BEGIN {
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
function AddNodesToTreeview($xElement, $fNode){
$allEl = $xElement.ChildNodes
foreach ($xEl in $allEl){
$tn = new-object System.Windows.Forms.TreeNode
if($xEl.HasChildNodes){
$tn.Text = $xEl.OuterXml.Split(">")[0] + ">"
$tn.Tag = $xEl.OuterXml
[void]$fNode.Nodes.Add($tn)
AddNodesToTreeview -xElement $xEl -fNode $tn
}else{
$tn.Text = $xEl.OuterXml
$tn.Tag = $xEl.OuterXml
[void]$fNode.Nodes.Add($tn)
}
}
}
}
PROCESS{
[xml]$xml = Get-Content $XMLPath
$FORM = new-object Windows.Forms.Form
$FORM.Size = new-object System.Drawing.Size(600,310)
$FORM.text = "Form-DisplayDirExplorer"
$FORM = new-object Windows.Forms.Form
$FORM.Size = new-object System.Drawing.Size(600,310)
$FORM.text = "XML - $XMLPath"
$TREEVIEW = new-object windows.forms.TreeView
$TREEVIEW.size = new-object System.Drawing.Size(595,269)
$TREEVIEW.Anchor = "top, left, bottom"
$TREEVIEW.add_NodeMouseDoubleClick({
[Windows.Forms.Clipboard]::SetText($TREEVIEW.SelectedNode.Tag.ToString());
})
$firstElement = $xml.$RootElementName
$tn = new-object System.Windows.Forms.TreeNode
$tn.Text = $firstElement.OuterXml.Split(">")[0] + ">"
$tn.Tag = $firstElement.OuterXml
[void]$TREEVIEW.Nodes.Add($tn)
AddNodesToTreeview -xElement $firstElement -fNode $tn
$FORM.Controls.Add($TREEVIEW)
$FORM.ShowDialog()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment