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

Re: XML data to a label.text property

Post by mtartaglia »

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
}
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 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.
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 »

miketartaglia wrote: Wed Feb 20, 2019 6: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
mtartaglia
Posts: 101
Last visit: Mon Dec 19, 2022 11:45 am

Re: XML data to a label.text property

Post by mtartaglia »

jvierra wrote: Wed Feb 20, 2019 7:24 pm
miketartaglia wrote: Wed Feb 20, 2019 6: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.
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 »

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

Re: XML data to a label.text property

Post by mtartaglia »

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 ;)
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 »

If the textbox is named "$bindingURL" then the value of the textbox is "$bindingURL.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 »

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

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

Re: XML data to a label.text property

Post by mtartaglia »

jvierra wrote: Thu Feb 21, 2019 9: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.
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