Populate Powershell WPF Datagrid With XML.

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
Steven D.
Posts: 3
Joined: Mon Oct 01, 2018 4:30 am

Populate Powershell WPF Datagrid With XML.

Post by Steven D. » Mon Oct 01, 2018 4:50 am

Hello Scripting guys.

I'm working on a project based on reading several XML files, then populate them into datagrid.

I'm trying to write a function in powershell to read the XML:
You will find the code below:

Code: Select all

Function Read-XMLFile {
Param(
[parameter(Mandatory=$true)]
[String]$XmlDocument,
[String]$SelectedNode,
[String]$Property,
[String]$Property2,
)
#Use Get-Content to load the xml file as a XML object
[ xml ]$XMLContent = Get-Content -Path $XmlDocument

#Get all Object in the XML object
# SelectedNode = "applications/application"
# $list = $XMLcontent.SelectNodes($SelectedNode)

$XMLContent.Applications.Application |
ForEach-Object{
$row = New-Object PSObject
Add-Member -inputObject $row -memberType NoteProperty -name $Property -value $_.$Property
# Populate My datagrid :
$DataGrid.AddChild($row)
}
# $array = New-Object System.Collections.ArrayList
# $array.AddRange([array]$list)
# $DataGrid_Applications.ItemsSource=$array
# $DataGrid_Applications.Columns.Add($array)
}
$XMLfile = "F:\Deploy\Control\Applications.xml"

Read-XMLFile -XmlDocument $XMLfile -Property "Name"
[/size]

But when i call a second time my function with another property, the result is added but not properly.
Read-XMLFile -XmlDocument $XMLfile -Property "Version"

The result looks like that : (See Attachments.)

I Know if i want to display Name & Version corresponding values, i have to increment it in my foreach code like and maybe add existing conditions.

Code: Select all

ForEach-Object{
$row = New-Object PSObject
Add-Member -inputObject $row -memberType NoteProperty -name $Property -value $_.$Property
Add-Member -inputObject $row -memberType NoteProperty -name $Property2 -value $_.$Property2
# Populate My datagrid :
$DataGrid.AddChild($row)
}
But i want for example call a first XML to full up my first column and call a second XML to populate an another column in the same datagrid calling my function.

Do you have any idea to help me ?

Thank you for reading

Best regards.
Attachments
result_datagrid.JPG
result_datagrid.JPG (62.35 KiB) Viewed 470 times

User avatar
jvierra
Posts: 13503
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Populate Powershell WPF Datagrid With XML.

Post by jvierra » Mon Oct 01, 2018 5:29 am

You area adding rows and not columns. This is a relational data issue. You need to add all columns then locate the matching row and set the column value.

Steven D.
Posts: 3
Joined: Mon Oct 01, 2018 4:30 am

Re: Populate Powershell WPF Datagrid With XML.

Post by Steven D. » Mon Oct 01, 2018 6:03 am

Thank you for your answer.
I don't really know how to implement this. Do you have an example ?

Thanks for the help.

User avatar
jvierra
Posts: 13503
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Populate Powershell WPF Datagrid With XML.

Post by jvierra » Mon Oct 01, 2018 6:32 am

If you read the documentation and C# examples for the DGV you should find examples of how to update cells in a DGV. How you would code this for you current needs is up to you. We cannot easily guess at what you are trying to do.

I suggest that you start by defining the key relation between the files.

Steven D.
Posts: 3
Joined: Mon Oct 01, 2018 4:30 am

Re: Populate Powershell WPF Datagrid With XML.

Post by Steven D. » Mon Oct 01, 2018 7:21 am

Found !
Thanks

Locked