I am trying to fetch the values for <Attribute>
, <Condition>
and <Group>
tags. There can be multiple <Attribute>
tags, so I need to fetch the values separately.
I am trying to prepare a condition out the above tags. Something like below.
if (HireType -eq 'KI' -and HireType1 = 'KI1' ){
$Groups = 'A,B,C'
}
I tried to write the code with the hep of ChildNodes, but it is merging the texts if there are multiple tags.
XML
<UserData>
<FilterCriteria>
</FilterCriteria>
<GroupCriteria>
<Criteria>
<Attributes>
<Attribute>
<AttrName>HireType</AttrName>
<AttrValue>KI</AttrValue>
</Attribute>
<Attribute>
<AttrName>HireType1</AttrName>
<AttrValue>KI1</AttrValue>
</Attribute>
<Condition>and</Condition>
<Groups>A,B,C</Groups>
</Attributes>
</Criteria>
</GroupCriteria>
</UserData>
Code
[xml] $xml = Get-Content -Path C:\Users\Arun\Desktop\UserData.xml
$xml.UserData.GroupCriteria | foreach {
$_.Criteria | foreach {
$_.Attributes | foreach {
$_.ChildNodes | % {
$Name = $_.Name
$Value = $_.InnerText
write-host "$Name : $Value"
}
}
}
}
Output
Attribute : HireTypeKI
Attribute : HireType1KI1
Condition : and
Groups : A,B,C
Since the <Attribute>
tags are structured differently, you'll need to handle those different from the others:
foreach ($node in $xml.UserData.GroupCriteria.Criteria.Attributes.ChildNodes) {
switch ($node.Name) {
'Attribute' {
$Name = $node.AttrName
$Value = $node.AttrValue
}
default {
$Name = $node.Name
$Value = $node.InnerText
}
}
Write-Host "$Name : $Value"
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments