Reading an XML File

This topic contains 5 replies, has 3 voices, and was last updated by  Simon B 1 month ago.

  • Author
    Posts
  • #91622

    Simon B
    Participant

    Ok, I have a form that has a text box and a label on it 🙂 Now I know that I could just hard code the label and text box text, but what I would like to do is use an XML config file to do this for me. This way when the form is reused only the XML will need changing and not the ps script.

    I am currently using :-

    [xml]$display = Get-Content C:\test\config.xml

    To read the contents of the XML into $display and I can reference the text for the text box with

    $display.Settings.FormSettings.TextBody

    But this just puts the text on one line so the formatting is not very easy to read and also does not look very nice. What I would like to do is keep the carriage returns and tabbing as in the XML file I have tried in the XML but it is not working. Any ideas ??

    This is the form label

    What I would like is for this text to be
    on Multilines as I have typed them
    here

  • #91625

    Simon B
    Participant

    This is the form label

    What I would like is for this text to be
    on Multilines as I have typed them
    here

  • #91627

    Don Jones
    Keymaster

    This is a pretty standard challenge with GUIs, and you mainly run across it when creating a GUI that will display strings in different languages. There's no easy solution. You either need to do some wildly complex math to calculate label size, or size the label to hold the largest possible string. Labels also do not support rich formatting – they will not display carriage returns, tabs, or other white space; that's not what they're designed to do. Your closest approximation would be a multi-line, non-editable (read-only) text box, but that's a pretty nonstandard use of that control so it won't provide a consistent GUI experience. You could probably set the border style to make the text box look more like a label, I suppose. This just isn't a use case that the Windows GUI standards cover.

    An XML file also isn't the only way to do this; PowerShell's native "data sections" was designed for this use, particularly in localization. See https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_data_sections?view=powershell-5.1; they're easier to read than XML, if that's useful for you.

  • #91630

    Don Jones
    Keymaster

    Also, avoid being the first person to type a reply to a thread you started. It removes your post from the "posts with no replies" list, which a lot of us use to find questions that need answering. 🙂

  • #91690

    Maurice L
    Participant

    Instead of using Winforms I would suggest using WPF instead. With WPF you usually data-bind items from the view to code (this makes translating parts of the GUI with ease since you bind the GUI text or label to a property : by changing the property value you change the value in the GUI.).
    I've written multiple GUIs in the past using WPF with translation features.

    see https://smsagent.wordpress.com/2017/02/03/powershell-deepdive-wpf-data-binding-and-inotifypropertychanged/ for more info.

  • #91741

    Simon B
    Participant

    Thanks for the link data binding looks like a good way to go 🙂

You must be logged in to reply to this topic.