0

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

1 Answer 1

3

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"
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.