Below is my xml that is provided by a third party and cannot me altered.
C:\Temp\test.xml:
<?xml version="1.0" standalone="yes"?>
<parameters>
<setParameter name="ConnectionString" value="mongodb://svc-dev, connectTimeoutMS=300000&w=majority"/>
</parameters>
Below powershell command was meant to load the XML but it fails with error:
PS C:\Users\meuser> $xml = [xml](Get-Content "C:\Temp\test.xml")
Cannot convert value "System.Object[]" to type "System.Xml.XmlDocument". Error: "'=' is an unexpected token. The expected
token is ';'. Line 3, position 90."
At line:1 char:1
+ $xml = [xml](Get-Content "C:\Temp\test.xml")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvalidCastToXmlDocument
The bigger picture is that I wish to check if my xml is empty or contains entries for setParameter as below.
# Check if the XML contains any entries for setParameter
if ($xml.parameters.setParameter) {
# Check if all the name and value attributes are empty
$isEmpty = $xml.parameters.setParameter | ForEach-Object {
$_.name -eq "" -and $_.value -eq ""
} | Measure-Object -Maximum
if ($isEmpty.Maximum -eq 1) {
Write-Host "XML RESULTS: All the name and value attributes are empty."
#exit 1;
} else {
Write-Host "XML RESULTS: The XML contains GOOD entries for setParameter."
}
} else {
Write-Host "XML RESULTS: The XML does not contain any entries for setParameter."
exit 1;
}
Note: i cannot and do not wish to change the contents of the test.xml.
I may further want to read key-value in the xml make changes and save it to a new xml file.
Kindly help me overcome the error.