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:
- 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"
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.
- 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)
- }
Do you have any idea to help me ?
Thank you for reading
Best regards.