mahesh mahesh - 1 year ago 146
PowerShell Question

XML SelectSingleNode Is case sensitive

While verifying the id in XML file, it is acting as a case-sensitive. I have added "translate" to make it as case-insensitive. Following is my code:

$Config = "xmlfile"
[xml]$configxml = Get-Content $config
$siteId = "Test"
$Siteid = $Siteid.ToLower()

$siteinfo = $configxml.SelectSingleNode("/configuration/environment[translate(@id='$($siteId)'])");

Write-Host "id specified '$siteId' not found in '$config' Aborting..."
exit -1
Write-Host "site id is present"

I'm getting following error:

id specified 'Test' not found in 'xmlfile' Aborting...

Here is my xml file:


<!-- TEST -->
<environment id="TEST">


Can someone please suggest me possible solution how to make it as case-insensitive and how to use "translate"?

Answer Source

Instead of an XPATH, you could also select the nodes like properties:

$config = "xmlfile"
[xml]$configxml = Get-Content $config
$siteId = "Test"

$siteInfo = $configxml.configuration.environment | Where id -eq $siteId

This will also case-insensitive match your id...