mahesh mahesh - 3 months ago 20
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)'])");

if(!$siteinfo)
{
Write-Host "id specified '$siteId' not found in '$config' Aborting..."
exit -1
}
else
{
Write-Host "site id is present"
}


I'm getting following error:

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


Here is my xml file:

<configuration>

<!-- TEST -->
<environment id="TEST">
<client>ABC</client>
<type>Test</type>
<filetype>ALL</filetype>
<enable>yes</enable>
</environment>

</configuration>


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

Answer

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

Comments