XML to $ListBox to $label1.text etc.

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 8 years and 7 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
User avatar
jimbobukii
Posts: 52
Last visit: Mon Dec 04, 2017 9:59 am

XML to $ListBox to $label1.text etc.

Post by jimbobukii »

Hi

I was wondering if someone could help me/my client?
I have managed to pull from XML into a List box all the ComputerID from an XML file (example below) using
PowerShell Code
Double-click the code block to select all.
[xml]$xmlfile = Get-Content "\\server\share$\xmlfile.xml
$Computer = $xmlfile.Computers.Computer
Load-ListBox -ListBox $listbox1 -Items $($Computer.ComputerID)

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<Computers>
  <Computer>
    <UserID>gsmith</UserID>
    <ComputerID>GLOS-01</ComputerID>
	<ComputerIP>192.168.2.145</ComputerIP>
	<ComputerLastLogon>28-07-2015-10:08</ComputerLastLogon>
	<ComputerUpdateLevel>2</ComputerUpdateLevel>
  </Computer>
  <Computer>
    <UserID>tdailey</UserID>
    <ComputerID>BRIS-01451</ComputerID>
	<ComputerIP>192.168.1.86</ComputerIP>
	<ComputerLastLogon>16-08-2015-22:36</ComputerLastLogon>
	<ComputerUpdateLevel>1</ComputerUpdateLevel>
  </Computer>
  ...
</Computers>
Basically they are after the <UserID> to be displayed in $label1.text, <ComputerIP> to be displayed in $LinkLabel1.text
I have tried using ....SelectedItem.... but no good!
There are a number of other bits that will be done but I cant figure how to pull <UserID>
XML above is a basic (with additional information removed) XML file that will have additional information added as and when the info is updated (XML created by other app).

Thanks in advance
User avatar
SAPIEN Support Forums
Posts: 945
Last visit: Thu Oct 22, 2015 1:10 pm

XML to $ListBox to $label1.text etc.

Post by SAPIEN Support Forums »

This is an automated post. A real person will respond soon.

Thank you for posting, jimbobukii.

Here are some hints to help you get an accurate and complete answer to your question.

Ask in the best forum: If you asked in the wrong forum, just copy your question to the right forum.

Anticipate follow-up questions!

Did you remember to include the following?
  • 1. Product, version and build
    2. 32 or 64 bit product
    3. Operating system, e.g. Windows 7 64 bit.
    4. Attach a screenshot, if applicable
    5. Attach logs, crash reports, etc., in a ZIP file
If not, please take a moment to edit your original post or reply to this one.

*** Make sure you do not post any licensing information ***
User avatar
dan.potter
Posts: 709
Last visit: Wed Nov 14, 2018 11:39 am

Re: XML to $ListBox to $label1.text etc.

Post by dan.potter »

$label1.text = $xmlfile.computers.Computer.userid

note there are two so you'll have to figure out which one to assign to the label.
Last edited by dan.potter on Mon Aug 24, 2015 10:48 am, edited 1 time in total.
User avatar
jimbobukii
Posts: 52
Last visit: Mon Dec 04, 2017 9:59 am

Re: XML to $ListBox to $label1.text etc.

Post by jimbobukii »

dan.potter wrote:$label1.text = $xmlfile.computers.Computer.userid
Doing this will present "ALL" entries within userid
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 $ListBox to $label1.text etc.

Post by jvierra »

Your basic problem is that you have multiple computers. Each one has a different "UserID". so assigning one to a label makes no sense.

YOU can store the whole XML in the ListBox and just display the computerIDs. You can then set a label or textbox to the userid of the selected computer. Is that what you are trying to do?
User avatar
jimbobukii
Posts: 52
Last visit: Mon Dec 04, 2017 9:59 am

Re: XML to $ListBox to $label1.text etc.

Post by jimbobukii »

jvierra wrote:Your basic problem is that you have multiple computers. Each one has a different "UserID". so assigning one to a label makes no sense.

YOU can store the whole XML in the ListBox and just display the computerIDs. You can then set a label or textbox to the userid of the selected computer. Is that what you are trying to do?
Thank you for your quick response, but I am unsure on how to proceed with your answer, would you mind posting an example?

TIA.
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 $ListBox to $label1.text etc.

Post by jvierra »

Here is how to convert an XML object to PS object that can be loaded into a listbox:
PowerShell Code
Double-click the code block to select all.
$txt=@'
<?xml version="1.0" encoding="utf-8"?>
<Computers>
  <Computer>
    <UserID>gsmith</UserID>
    <ComputerID>GLOS-01</ComputerID>
   <ComputerIP>192.168.2.145</ComputerIP>
   <ComputerLastLogon>28-07-2015-10:08</ComputerLastLogon>
   <ComputerUpdateLevel>2</ComputerUpdateLevel>
  </Computer>
  <Computer>
    <UserID>tdailey</UserID>
    <ComputerID>BRIS-01451</ComputerID>
   <ComputerIP>192.168.1.86</ComputerIP>
   <ComputerLastLogon>16-08-2015-22:36</ComputerLastLogon>
   <ComputerUpdateLevel>1</ComputerUpdateLevel>
  </Computer>
</Computers>
'@
$xml=[xml]$txt

$xml.SelectNodes('//Computer') |select computerid,userid,computerip, ComputerLastLogon,ComputerUpdateLevel
Run this at a prompt to see the objects that are created.

To load a listbox with an object I would do this:
PowerShell Code
Double-click the code block to select all.
$items=$xml.SelectNodes('//Computer') |select computerid,userid,computerip, ComputerLastLogon,ComputerUpdateLevel

$listbox1.DataSource=[collections.arraylist]$items
$listbox1.DisplayMember='ComputerID'
To get the user in the change event

$label.Text=$listbox1.SelectedItem.UserID
This topic is 8 years and 7 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