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.
User avatar
miketartaglia
Posts: 91
Joined: Mon Feb 02, 2015 7:44 am

XML data to a label.text property

Post by miketartaglia » Wed Feb 20, 2019 9:11 am

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
Joined: Thu Aug 18, 2011 4:56 am

Re: XML data to a label.text property

Post by davidc » Wed Feb 20, 2019 9:50 am

[TOPIC MOVED TO POWERSHELL GUIS FORUM BY MODERATOR]
David
SAPIEN Technologies, Inc.

User avatar
davidc
Posts: 5913
Joined: Thu Aug 18, 2011 4:56 am

Re: XML data to a label.text property

Post by davidc » Wed Feb 20, 2019 9:53 am

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
miketartaglia
Posts: 91
Joined: Mon Feb 02, 2015 7:44 am

Re: XML data to a label.text property

Post by miketartaglia » Wed Feb 20, 2019 10:23 am

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.

User avatar
jvierra
Posts: 13718
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: XML data to a label.text property

Post by jvierra » Wed Feb 20, 2019 11:52 am

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

$detailsLBL.text = $Node.name.'#text'

User avatar
miketartaglia
Posts: 91
Joined: Mon Feb 02, 2015 7:44 am

Re: XML data to a label.text property

Post by miketartaglia » Wed Feb 20, 2019 12:13 pm

$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.

User avatar
jvierra
Posts: 13718
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: XML data to a label.text property

Post by jvierra » Wed Feb 20, 2019 12:45 pm

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
miketartaglia
Posts: 91
Joined: Mon Feb 02, 2015 7:44 am

Re: XML data to a label.text property

Post by miketartaglia » Wed Feb 20, 2019 1:09 pm

this is the output you requested

xml Servers
--- -------
version="1.0" encoding="utf-8" Servers

User avatar
jvierra
Posts: 13718
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: XML data to a label.text property

Post by jvierra » Wed Feb 20, 2019 2:02 pm

Please post the XML of you file. What we are seeing says that there are more issues with the file.

User avatar
jvierra
Posts: 13718
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: XML data to a label.text property

Post by jvierra » Wed Feb 20, 2019 2:06 pm

What was the result of my code?

Locked