Hi,
Am in the process of building a GUI interface with the help of Powershell Studio 2014 that displays data from xml file.
My Requirement:
1. Clicking on the Browse button on the windows form will open a file dialog box for selecting the xml file.
XML File:
<?xml version="1.0"?> <root> <Employee> <Records> <Record> <EmpID>100</EmpID> <EmpName>Naveen</EmpName> <EmpAddress><![CDATA[#10, TNHB, EWS QUARTERS (PO), INDIA]]></EmpAddress> <CountryID>7</CountryID> </Record> <Record> <EmpID>101</EmpID> <EmpName>Kumar</EmpName> <EmpAddress><![CDATA[#29, T.R Nagar, EWS QUARTERS (PO), INDIA]]></EmpAddress> <CountryID>7</CountryID> </Record> <Record> <EmpID>102</EmpID> <EmpName>Nagareddy</EmpName> <EmpAddress><![CDATA[#201, Lake View, EWS QUARTERS (PO), INDIA]]></EmpAddress> <CountryID>7</CountryID> </Record> </Records> </Employee> <Country> <Records> <Record> <CountryID>7</CountryID> <CountryName>India</CountryName> <CountryGUID><![CDATA[4e6wiyj2gqwbuyswx92pfvvams5pppjw@]]></CountryGUID> </Record> </Records> </Country> </root>
2. List of tables are populated to combo-box, DropDownStyle property is DropDownList.
3. Data is displayed in the DataGridView control when a table is selected from DropDown.
4. DataGridView works better if there are multiple records in table for example Employee table in XML file and errors out for a single record for example Country table in XML file.
Combo-box script:
$combobox1_SelectedIndexChanged={ $array = New-Object System.Collections.ArrayList if ($combobox1.Text) { $tablename = $combobox1.Text $index = [array]::IndexOf($global:tablelist, $tablename) $Results = foreach ($x in $global:xml.root.($global:tablelist[$index]).Records.Record) { $cols = $x.childnodes.localname $props = New-Object PSObject For ($i = 0; $i -lt $cols.count; $i++) { $props | Add-Member $cols[$i] $(If (($x.($cols[$i]).innertext) -or ($x.($cols[$i]).innertext -eq "")) { $($x.($cols[$i]).innertext) } else { $x.($cols[$i]) }) }$props } $array.AddRange($Results) $datagridview1.DataSource = $array } }
Gives out below error for Country table
ERROR: Cannot convert argument "c", with value: "@{CountryID=7; CountryName=India; CountryGUID=4e6wiyj2gqwbuyswx92pfvvams5pppjw@}", for "AddRange" to type ERROR: "System.Collections.ICollection": "Cannot convert the "@{CountryID=7; CountryName=India; CountryGUID=4e6wiyj2gqwbuyswx92pfvvams5pppjw@}" value of type ERROR: "System.Management.Automation.PSCustomObject" to type "System.Collections.ICollection"." Detailed Info Test.psf (153): ERROR: At Line: 153 char: 4 ERROR: + $array.AddRange($Results) ERROR: + ~~~~~~~~~~~~~~~~~~~~~~~~~ ERROR: + CategoryInfo : NotSpecified: (:) [], MethodException ERROR: + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument ERROR:
Let me know for full code.
Please kindly suggest me in overcoming the above error.
Thanks in Advance.
Regards,
Naveen. N