0

New to Powershell and XAML.

I'm trying to setup the content of a Button to read a specific XML file (C:\Folder\Number.xml) Just to have the chance to change the content of a button of my application directly from an XML instead of changing code in Powershell, and also to show some variable information.

I'm getting the content of the xml file with this:

$xmlFilePathLOCAL = 'C:\Folder\Number.xml'
$NumberXml = [xml](Get-Content $xmlFilePathLOCAL)

"Cell1" button should get the content from the Number tag in the XML file:

XML EXAMPLE HERE:

<?xml version="1.0" encoding="utf-16"?>
<data>
  <Number>1</Number>
  <Vocals>e</Vocals>
  <Days>Monday</Days>
  <Fruits>Apples</Fruits>
</data>

I tried to set a Binding and multiple things as the following. It didn't give me any error but at the same time the Button is empty:

<Button Name="Cell1" Grid.Row="0" Grid.Column="0"  Opacity="1"  Background="#808080"  FontFamily="Impact" FontSize="30" Foreground="White">
    <TextBlock Text="{Binding ElementName=NumberXml, Path=data/Number}" FontSize="60" FontFamily="Impact"/>
</Button>

What I'm doing wrong? Many thanks to all your help!

This is part of the code:

$xmlFilePathLOCAL = 'C:\Folder\Number.xml'
$NumberXml = [xml](Get-Content $xmlFilePathLOCAL)

#Show-Process "overlay"
$inputXML = @"
<Window 
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
 x:Name="Window" 
  WindowStyle = "None" WindowState="Maximized"
 ResizeMode = "NoResize" Title = "overlay" AllowsTransparency = "True" Background = "Transparent" Opacity = "1" Topmost = "True" >
  <Grid x:Name = "Grid" Background = "Transparent" ShowGridLines="False" >
  <Grid.ColumnDefinitions>
    <ColumnDefinition Width="*" />
    <ColumnDefinition Width="*" />
  </Grid.ColumnDefinitions>
  <Grid.RowDefinitions>
  <RowDefinition Height="*" />
  </Grid.RowDefinitions>  
<Button Name="Cell1" Grid.Row="0" Grid.Column="0"  Opacity="1"  Background="#808080"  FontFamily="Impact" FontSize="30" Foreground="White" Content="TEXT TO GET FROM XML FILE"/>
<Button Name="EXIT" Grid.Row="0" Grid.Column="1"  Opacity="1" Background="RED" FontFamily="Impact" FontSize="30" Foreground="White" Content="EXIT"/>
</Grid>
</Window>
"@
$inputXML = $inputXML -replace 'mc:Ignorable="d"','' -replace "x:N",'N' -replace '^<Win.*', '<Window'
[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
[xml]$XAML = $inputXML
#Read XAML
Marco
  • 3
  • 3
  • If you simply want to insert the file's text as-is, replace `TEXT TO GET FROM XML FILE` with `$(Get-Content -Raw C:\Folder\Number.xml)`; for the rules of PowerShell's string expansion (interpolation), see [this answer](https://stackoverflow.com/a/40445998/45375). – mklement0 Dec 04 '20 at 13:53
  • Also, it's better to use the [`Add-Type`](https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/add-type) cmdlet to load the WPF assemblies: `Add-Type -AssemblyName PresentationCore, PresentationFramework` – mklement0 Dec 04 '20 at 13:54
  • @mklement0 Edited. Thanks again for your time Sorry if I did something incorrect. Here a different example of the XML files and the data I could get. Let's say I want to choose to show the Number only, and leave the other tag for differents buttons. 1 e Monday Apples – Marco Dec 04 '20 at 15:08
  • 1
    `$TheNumberFromXml = ([xml](Get-Content -Path 'D:\Test\number.xml' -Raw)).data.number` – Theo Dec 04 '20 at 15:09

1 Answers1

0

If the only value in your XML file is the <Number> tag , you can use the line I have commented to get the value in a variable.

$TheNumberFromXml = ([xml](Get-Content -Path 'D:\Test\number.xml' -Raw)).data.number

If however the file has more tags you want to get the values for, it might be easiest to just read the file once and have PowerShell parse it into a nice object. Then get whatever you need from that when it is appropriate.

Suppose (as in your comment) the file looks like this:

<?xml version="1.0" encoding="utf-16"?>
<data>
  <Number>1</Number>
  <Vocals>e</Vocals>
  <Days>Monday</Days>
  <Fruits>Apples</Fruits>
</data>

Then in your script read in once

[xml]$TheXmlValues = Get-Content -Path 'D:\Test\number.xml' -Raw

Then get what you need for any specific tag by doing

$number = $TheXmlValues.data.number
$vocals = $TheXmlValues.data.vocals
$days   = $TheXmlValues.data.days
$fruits = $TheXmlValues.data.fruits

Using the 'dotted' method to get the values from tags is Case-Insensitive

Theo
  • 57,719
  • 8
  • 24
  • 41