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

Re: XML data to a label.text property

Post by miketartaglia » Wed Feb 20, 2019 7:53 pm

My original question was how to get that output of just the Name from that Instance node into the $detailsLBL control text property. It still doesn't work. It still shows up as BLANK.

$buttonSEARCH_Click= {
#TODO: Place custom script here
[xml]$file = (Get-Content 'c:\test\programdata\Server Configuration.xml')
$detailsLBL.Text = $file.SelectSingleNode("//Instance[BindingHostName='$BindingURL']").Name
}

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

Re: XML data to a label.text property

Post by jvierra » Wed Feb 20, 2019 8:22 pm

Without a good and correct example of the XML I cannot help you.

My example works. Run this and you will see that it works.

Code: Select all

[xml]$xml = @'
<Servers>
    <Server>
        <Instances>
            <Instance>
                <Name>billybob</Name>
                <Version>8.26.0.0</Version>
                <InstalledVersion>8.26.0.0</InstalledVersion>
                <LicenseKey>111-222-333-44444</LicenseKey>
                <AppPoolName>billybob</AppPoolName>
                <BindingHostName>billybob.nowhere.com</BindingHostName>
                <UseSSLBinding>false</UseSSLBinding>
                <ShipmentServerEndPoint />
                <BindingSSLCertificate>
                <RequireServerNameIdentification>false</RequireServerNameIdentification>
                <Issuer />
                <SerialNumber />
                <Storage />
                <SslStatus>Synchronized</SslStatus>
                </BindingSSLCertificate>
                <AuthenticationMode>Forms</AuthenticationMode>
            </Instance>
            <Instance>
                <Name>bobbyjoe</Name>
                <Version>8.26.0.0</Version>
                <InstalledVersion>8.26.0.0</InstalledVersion>
                <LicenseKey>111-222-333-44444</LicenseKey>
                <AppPoolName>bobbyjoe</AppPoolName>
                <BindingHostName>bobbyjoe.nowhere.com</BindingHostName>
                <UseSSLBinding>false</UseSSLBinding>
                <ShipmentServerEndPoint />
                <BindingSSLCertificate>
                <RequireServerNameIdentification>false</RequireServerNameIdentification>
                <Issuer />
                <SerialNumber />
                <Storage />
                <SslStatus>Synchronized</SslStatus>
                </BindingSSLCertificate>
                <AuthenticationMode>Forms</AuthenticationMode>
            </Instance>
        </Instances>
    </Server>
</Servers>
'@
$BindingURL = 'billybob.nowhere.com'
$textName = $xml.SelectSingleNode("//Instance[BindingHostName='billybob.nowhere.com']").Name
$textName
Without accurate information about your XML there is no way to know why this would fail. If you have namespaces in the XML then the namespaces must be resolved to access the contents of the XML.

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

Re: XML data to a label.text property

Post by jvierra » Wed Feb 20, 2019 8:24 pm

miketartaglia wrote:
Wed Feb 20, 2019 7:53 pm
My original question was how to get that output of just the Name from that Instance node into the $detailsLBL control text property. It still doesn't work. It still shows up as BLANK.

$buttonSEARCH_Click= {
#TODO: Place custom script here
[xml]$file = (Get-Content 'c:\test\programdata\Server Configuration.xml')
$detailsLBL.Text = $file.SelectSingleNode("//Instance[BindingHostName='$BindingURL']").Name
}
In this example the variable is never set in the code. It is null so you will get back nothing.

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

Re: XML data to a label.text property

Post by miketartaglia » Thu Feb 21, 2019 6:46 am

jvierra wrote:
Wed Feb 20, 2019 8:24 pm
miketartaglia wrote:
Wed Feb 20, 2019 7:53 pm
My original question was how to get that output of just the Name from that Instance node into the $detailsLBL control text property. It still doesn't work. It still shows up as BLANK.

$buttonSEARCH_Click= {
#TODO: Place custom script here
[xml]$file = (Get-Content 'c:\test\programdata\Server Configuration.xml')
$detailsLBL.Text = $file.SelectSingleNode("//Instance[BindingHostName='$BindingURL']").Name
}
In this example the variable is never set in the code. It is null so you will get back nothing.
$BindingURL is set in a text box. Sorry I forgot to explain this. When someone clicks the SEARCH button it runs the $buttonSEARCH_Click code. I must be doing something wrong with the GUI as it works fine in a powershell command window.

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

Re: XML data to a label.text property

Post by jvierra » Thu Feb 21, 2019 7:07 am

Why are you using a variable when the data is in a textbox. Just use the textbox.

I suspect you have a scope issue which is the reason we don't use interim variables in a form. Check the variable. It is likely null.

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

Re: XML data to a label.text property

Post by miketartaglia » Thu Feb 21, 2019 7:30 am

The textbox is named BindingURL. I thought that was considered a variable when using it in code. My mistake. So since $BindingURL is NULL that makes sense. I think I am back in rookie status then ;)

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

Re: XML data to a label.text property

Post by jvierra » Thu Feb 21, 2019 7:47 am

If the textbox is named "$bindingURL" then the value of the textbox is "$bindingURL.Text".

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

Re: XML data to a label.text property

Post by miketartaglia » Thu Feb 21, 2019 10:32 am

The problem is that it still shows up as NULL or blank. It starts off with a value of "TEXT" (I set that up for troubleshooting) after I click the button (the code you have already seen), it switches to NULL. So we probed that your code works, but I can not make the value of that specific output to be in the $detailsLBL value.

Does that make sense?

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

Re: XML data to a label.text property

Post by jvierra » Thu Feb 21, 2019 10:39 am

No that doesn't make any sense.

What has a value of "TEXT"?
What switches to NULL?
What code have you changed?
What is $detailsBL?

Without your code none of this makes any sense.
What is in the variable "$BindingURL'"? It is null.

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

Re: XML data to a label.text property

Post by miketartaglia » Thu Feb 21, 2019 10:50 am

jvierra wrote:
Thu Feb 21, 2019 10:39 am
No that doesn't make any sense.

What has a value of "TEXT"? detailsLBL - This is the label where I want the value of the XML code you wrote to show as the .NAME at the end of the XML line.
What switches to NULL? detailsLBL switches to NULL instead of the .NAME above.
What code have you changed? None
What is $detailsBL? this is a label that I want to display the value mentioned above.

Without your code none of this makes any sense. You should already have the code in this long thread ;)
What is in the variable "$BindingURL'"? It is null. This is the value of a text box that I am typing in a URL that needs to be pulled out of the XML file and set the detailsLBL.TEXT to the .NAME mentioned above.

Locked