Dear Powershell fellows,
i have a (maybe) very simple problem, but i have no idea how to solve it. I want to use a String Variable, that contains a single quote, within a XPath notation. I use the CMDlet Select-Xml. If there is no single quote within the String, Select-Xml is working completly fine. But if there is one single quote (for example in: don't) it crashes my script. Let me show you in detail.
Problem
$Name = "Dont display" ##is working completly fine
(Select-Xml -Path "C:\SomePath\Somexml.xml" -XPath "//q1:Options[q1:Name = '$Name']" -Namespace $namespace).Node.InnerText ##is working completly fine
$Name = "Don't display" ##crashes script
(Select-Xml -Path "C:\SomePath\Somexml.xml" -XPath "//q1:Options[q1:Name = '$Name']" -Namespace $namespace).Node.InnerText ##crashes script
The error output of powershell is:
Select-Xml : '//q1:Options[q1:Name = 'Don't display']' has an invalid token.
At line:251 char:41
+ ... ing_Name = (Select-Xml -Path "C:\SomePath\Somexml.xml" -XPath ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Select-Xml], XPathException
+ FullyQualifiedErrorId : System.Xml.XPath.XPathException,Microsoft.PowerShell.Commands.SelectXmlCommand
What i tried so far
Of course i tried different quotations, such as:
$Name = """Don't display"""
$Name = '"Don't display"'
$Name = "'Don't display'"
$Name = "'Don't display'"
$Name = 'Don't display'
$Name = ''Don't display''
It seems like, there might be a problem with powershell quotation rules and XPath notation.
But maybe anyone of you guys have an idea on how to solve it.
Thank you very much