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: 97
Meble kuchenne na zamówienie - na wymiar - Wrocław
Joined: Mon Feb 02, 2015 7:44 am

XML data to a label.text property

Post by miketartaglia »

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 »

[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 »

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

Re: XML data to a label.text property

Post by miketartaglia »

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: 14203
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Re: XML data to a label.text property

Post by miketartaglia »

$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: 14203
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Re: XML data to a label.text property

Post by miketartaglia »

this is the output you requested

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

jvierra
Posts: 14203
Joined: Tue May 22, 2007 9:57 am
Contact:

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: 14203
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: XML data to a label.text property

Post by jvierra »

What was the result of my code?

Locked