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.
This topic is 5 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
Steven D.
Posts: 3
Last visit: Wed Oct 03, 2018 7:22 am

Populate Powershell WPF Datagrid With XML.

Post by Steven D. »

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:

  1. Function Read-XMLFile {
  2.     Param(
  3.         [parameter(Mandatory=$true)]
  4.         [String]$XmlDocument,
  5.         [String]$SelectedNode,
  6.         [String]$Property,
  7.     [String]$Property2,
  8.     )
  9.     #Use Get-Content to load the xml file as a XML object
  10.     [ xml ]$XMLContent = Get-Content -Path $XmlDocument
  11.  
  12.     #Get all Object in the XML object
  13.     # SelectedNode = "applications/application"
  14.     # $list = $XMLcontent.SelectNodes($SelectedNode)
  15.  
  16.     $XMLContent.Applications.Application |
  17.       ForEach-Object{
  18.        $row = New-Object PSObject
  19.         Add-Member -inputObject $row -memberType NoteProperty -name $Property -value $_.$Property
  20.                 # Populate My datagrid :
  21.         $DataGrid.AddChild($row)
  22.       }
  23.     # $array = New-Object System.Collections.ArrayList    
  24.     # $array.AddRange([array]$list)    
  25.     # $DataGrid_Applications.ItemsSource=$array
  26.     # $DataGrid_Applications.Columns.Add($array)
  27. }
  28. $XMLfile = "F:\Deploy\Control\Applications.xml"
  29.  
  30. 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.
  1.       ForEach-Object{
  2.        $row = New-Object PSObject
  3.         Add-Member -inputObject $row -memberType NoteProperty -name $Property -value $_.$Property
  4.                 Add-Member -inputObject $row -memberType NoteProperty -name $Property2 -value $_.$Property2
  5.                 # Populate My datagrid :
  6.         $DataGrid.AddChild($row)
  7.       }
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 2975 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Populate Powershell WPF Datagrid With XML.

Post by jvierra »

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
Last visit: Wed Oct 03, 2018 7:22 am

Re: Populate Powershell WPF Datagrid With XML.

Post by Steven D. »

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

Thanks for the help.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Populate Powershell WPF Datagrid With XML.

Post by jvierra »

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
Last visit: Wed Oct 03, 2018 7:22 am

Re: Populate Powershell WPF Datagrid With XML.

Post by Steven D. »

Found !
Thanks
This topic is 5 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked