I am working on an XML content (below) and wanted to iterate the value of the Student element and corresponding value of Gender, and then finally check if the student name is Anne, print the Gender as Female.
However when I try using below script, the output I get is-
Anne is Male
Anne is Male
Anne is Female
What could be the issue here? Could I get some help here?
XML content:
?xml version="1.0"?>
<Objects>
<Object Type="System.Management.Automation.CustomObject">
<Property Name="Gender" Type="Application.String">Male</Property>
<Property Name="Student" Type="Microsoft.Application.Service">John</Property>
</Object>
<Object Type="System.Management.Automation.CustomObject">
<Property Name="Gender" Type="Application.String">Male</Property>
<Property Name="Student" Type="Microsoft.Application.Service">Charles</Property>
</Object>
<Object Type="System.Management.Automation.CustomObject">
<Property Name="Gender" Type="Application.String">Female</Property>
<Property Name="Student" Type="Microsoft.Application.Service">Anne</Property>
</Object>
</Objects>
Here is the script:
[xml]$file = Get-Content C:\xml-report.xml
foreach ($obj in $file.Objects.Object.Property) {
if('Student'-contains $obj.Name) {
$name = $obj.'#text'
}
if('Gender' -contains $obj.Name) {
$gend = $obj.'#text'
}
if ($name = "Anne") {
Write-Host $name "is" $gend
}
}
Export-CliXml. Why not useImport-CliXml?ConvertTo-Xml, which AFAIK use different format thanExport-CliXmland does not haveConvertFrom/Importcommand.