XML data to a label.text property

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 1 month 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
mtartaglia
Posts: 101
Last visit: Mon Dec 19, 2022 11:45 am

XML data to a label.text property

Post by mtartaglia »

To help you better we need some information from you.

*** Please fill in the fields below. If you leave fields empty or specify 'latest' rather than the actual version your answer will be delayed as we will be forced to ask you for this information. ***

Product, version and build: 2019 5.6.159
32 or 64 bit version of product: 64
Operating system: Windows 10
32 or 64 bit OS: 64

I am having trouble making a label have the correct text that comes from an XML file.

Code:
$buttonSEARCH_Click={
#TODO: Place custom script here
[xml]$file = (Get-Content 'c:\test\programdata\Server Configuration.xml')
$Node = $file.servers.server.Instances.Instance | Where-Object{$_.BindingHostName -eq $BindingURL}
$detailsLBL.text = $Node.name
}
END of code

Maybe it's just because I haven't used Powershell Studio in a long time. Any help would be appreciated. I have already confirmed that $Node variable works in regular powershell window, so I guess it's just a matter of syntax.
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: XML data to a label.text property

Post by davidc »

[TOPIC MOVED TO POWERSHELL GUIS FORUM BY MODERATOR]
David
SAPIEN Technologies, Inc.
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: XML data to a label.text property

Post by davidc »

You can run into trouble if you are getting an array back. Try using the Out-String cmdlet:

Code: Select all

$detailsLBL.text = $Node.name | Out-String
You can also run into the case where you are getting nothing back as well.
David
SAPIEN Technologies, Inc.
User avatar
mtartaglia
Posts: 101
Last visit: Mon Dec 19, 2022 11:45 am

Re: XML data to a label.text property

Post by mtartaglia »

I already tried that before. the fact is I am getting nothing back. I start with detailsLBL.text with a default value of 'TEXT'. When it gets to reassign a value it assigns it as blank.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: XML data to a label.text property

Post by jvierra »

You have to extract the '#text' node if "Name" is an element.

$detailsLBL.text = $Node.name.'#text'
User avatar
mtartaglia
Posts: 101
Last visit: Mon Dec 19, 2022 11:45 am

Re: XML data to a label.text property

Post by mtartaglia »

$detailsLBL is still equal to BLANK

I'm wondering if the .name is not an element for some reason? It worked fine in powershell ISE.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: XML data to a label.text property

Post by jvierra »

Without a sample of you XML it is not possible to know why this is happening. If you have multiple nodes then the code will need to be different.

Run the code at a PS prompt and inspect what is returned.

Run this at a prompt:

[xml]$xml = (Get-Content 'c:\test\programdata\Server Configuration.xml')
$xml.SelectNodes('//Instance/BindingHostName')


What does it return?

Post a single instance XML.
User avatar
mtartaglia
Posts: 101
Last visit: Mon Dec 19, 2022 11:45 am

Re: XML data to a label.text property

Post by mtartaglia »

this is the output you requested

xml Servers
--- -------
version="1.0" encoding="utf-8" Servers
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: XML data to a label.text property

Post by jvierra »

Please post the XML of you file. What we are seeing says that there are more issues with the file.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: XML data to a label.text property

Post by jvierra »

What was the result of my code?
This topic is 5 years and 1 month 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