XML to Datagrid

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 11 years and 3 weeks 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
User avatar
bheidemann
Posts: 19
Last visit: Mon Dec 09, 2013 9:11 am

XML to Datagrid

Post by bheidemann »

I would like to display xml data in a datagrid and possible write the changes back to the xml. I found some examples on how to do this using arrays but I cannot get it to work in my code.

Code: Select all

function get-configxml { 
	[xml]$fileConfig = (get-content "c:\temp\WatchedFolder.cfg")
	$script:configInfo = ($fileConfig.watch_folders.watch_folder_pair | select in,out,error)
	$datagrid1.DataSource = $configInfo
	$formCVisionServerStatus.Refresh
}

The error I get is

ERROR: Exception setting "DataSource": "Complex DataBinding accepts as a data source either an IList or an IListSource."
CVisionStatus.pff (17): ERROR: At Line: 17 char: 2


I was trying to use this site as an example:
http://blogs.technet.com/b/heyscripting ... -2010.aspx

Any suggestions?
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: XML to Datagrid

Post by davidc »

I moved the post to the appropriate forum.

A grid is not a good way to represent an XML file. You might want to look into the TreeView control.

Also if you want using Data binding you will need to use a DataTable or an ArrayList.


David
David
SAPIEN Technologies, Inc.
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: XML to Datagrid

Post by davidc »

Here is an example how to use an ArrayList:
PowerShell Code
Double-click the code block to select all.
$results = Get-WmiObject Win32_Process -Namespace "Root\CIMV2"
#Convert to ArrayList so we can Data Bind the results 
$array = New-Object System.Collections.ArrayList
$array.AddRange($results)
$datagridviewResults.DataSource = $array
David
David
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: XML to Datagrid

Post by jvierra »

David - FYI; The following line will work assuming it is returning the right elements. We just need to cast it as an arraylist:

$configInfo=$fileConfig.watch_folders.watch_folder_pair | select in,out,error

THe 'select', if it works correctly, will make teh nodes appear as a list.

Writing back is a matter of attaching the nodes to the rows and then processing the grid after the edits.
User avatar
bheidemann
Posts: 19
Last visit: Mon Dec 09, 2013 9:11 am

Re: XML to Datagrid

Post by bheidemann »

Thank you for your responses. I modified the code a bit to use an array as so:

Code: Select all

function get-configxml { 
$array = New-Object System.Collections.ArrayList
[xml]$fileConfig = (get-content "c:\temp\WatchedFolder.cfg")
$script:configInfo = ($fileConfig.watch_folders.watch_folder_pair | select in,out,error)
$array.AddRange($configInfo)
#$datagrid1.DataSource = $configInfo
#$formCVisionServerStatus.Refresh
}
Note: I realize the last two lines are commented out, this is just to test the actual code.

The error I get now is:

Cannot convert argument "c", with value: "@{in=\\share\Import;
out=\\share\Export; error=\\share\Error}", for "AddRange" to type
"System.Collections.ICollection": "Cannot convert the "@{in=\\share\Import;
out=\\share\Export; error=\\share\Error}" value of type
"Selected.System.Xml.XmlElement" to type "System.Collections.ICollection"."
At line:5 char:1
+ $array.AddRange($configInfo)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument


All I want to do is be able to modify the in, out and error directories of the xml config file. If I am way off base please let me know. Thank you guys.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: XML to Datagrid

Post by jvierra »

We need an example of your XML file. Without it it is very hard to show you what you need to do or to determine if it is possible.

Please post an example file as an attachment. Using the 'Upload attachment' link on this page.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: XML to Datagrid

Post by jvierra »

The errors you are getting is becuse you are passing XML nodes to teh grid and not a table of strings or other useful objects. We need to produce a correct XSLT query to get the data in a usable form. A datagrid does not understand XML.
User avatar
bheidemann
Posts: 19
Last visit: Mon Dec 09, 2013 9:11 am

Re: XML to Datagrid

Post by bheidemann »

Here is the XML

Code: Select all

<watch_folders>
  <check_interval>5</check_interval>
  <multithread>0</multithread>
  <watch_folder_pair>
    <error_properties>C:\Program Files (x86)\something\comp_props.dat</error_properties>
    <log_file>C:\Program Files (x86)\something\watchedfolder.log</log_file>
    <processed_action>delete</processed_action>
    <processed_parameter></processed_parameter>
    <error>\\share\Barcodes\Error</error>
    <recursive>0</recursive>
    <file_extensions>tif;tiff;jpg;jpeg;pdf;</file_extensions>
    <in>\\share\Import</in>
    <out>\\share\Export</out>
    <command>"C:\Program Files (x86)\something\something.exe" -globaloff -m 1 -c ON -qualityc 20 -qualityg 20 -rscdwndpi 300 -rscinterp smartbicubic -rsgdwndpi 300 -rsginterp smartbicubic -rsbdwndpi 300 -rsbinterp smartbicubic -cconc -ccong -redirstderr -config "C:\ProgramData\something\ImgProc.xml" -annotation "C:\Scanning\Cover.xml" -in "/%in/" -out "/%out/.pdf"</command>
  </watch_folder_pair>
</watch_folders>
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: XML to Datagrid

Post by jvierra »

Here is the solution as an attachment PS1 file.

Here is the only code changed in the project.
PowerShell Code
Double-click the code block to select all.
$FormEvent_Load={
	#TODO: Initialize Form Controls here
$xml=[xml]@'
<watch_folders>
  <check_interval>5</check_interval>
  <multithread>0</multithread>
  <watch_folder_pair>
    <error_properties>C:\Program Files (x86)\something\comp_props.dat</error_properties>
    <log_file>C:\Program Files (x86)\something\watchedfolder.log</log_file>
    <processed_action>delete</processed_action>
    <processed_parameter></processed_parameter>
    <error>\\share\Barcodes\Error</error>
    <recursive>0</recursive>
    <file_extensions>tif;tiff;jpg;jpeg;pdf;</file_extensions>
    <in>\\share\Import</in>
    <out>\\share\Export</out>
    <command>"C:\Program Files (x86)\something\something.exe" -globaloff -m 1 -c ON -qualityc 20 -qualityg 20 -rscdwndpi 300 -rscinterp smartbicubic -rsgdwndpi 300 -rsginterp smartbicubic -rsbdwndpi 300 -rsbinterp smartbicubic -cconc -ccong -redirstderr -config "C:\ProgramData\something\ImgProc.xml" -annotation "C:\Scanning\Cover.xml" -in "/%in/" -out "/%out/.pdf"</command>
  </watch_folder_pair>
</watch_folders>
'@
     $list=$xml.watch_folders.watch_folder_pair |
          ForEach-Object{
               New-Object PsObject -Property @{
                                                  IN=$_.in;
                                                  OUT=$_.out;
                                                  ERROR=$_.error
                                             }
          }
     $array = New-Object System.Collections.ArrayList     
     $array.AddRange([array]$list)     
     $datagrid1.DataSource=$array 
}
IN your case you will load from a file after yu see how this works.

The update will be a litttle more involved but we will do that later,

See attachment.
Attachments
Demo-LoadXML.Export.ps1.txt
PS1 of demo script
(14.37 KiB) Downloaded 451 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: XML to Datagrid

Post by jvierra »

Here is the PFF file of the same code in case this is easier. On both just remove the TXT extension.

See attachment:
Attachments
Demo-LOadXML.pff.txt
PFF file of demo script
(6.97 KiB) Downloaded 463 times
This topic is 11 years and 3 weeks 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