Quantcast
Channel: /r/powershell – How to Code .NET
Viewing all articles
Browse latest Browse all 8793

XML: modify a child node if a sibling node has a particular value?

$
0
0

I am trying to put together a script that looks for a particular value in a named childnode and then replaces the value of a sibling node depending if that value is found

here is the structure of the XML that I am modifying

<List> <Items> <Item> <Attributes> <AttributeX>HasAValue</AttributeX> <AttributeY></AttributeY> </Attributes> <Attributes> <AttributeX>HasADifferentValue</AttributeX> <AttributeY></AttributeY> </Attributes> </Item> </Items> </List>

I want to look at <AttributeX> and if it finds “HasAValue”, change the value of its sibling <AttributeY> to “something”

I have worked it out as far as being able to change the value of AttributeX, but havent been able to change AttributeY.

$xml.SelectNodes(“//AttributeX”) | Where-Object { $.’#text’ -match ‘HasAValue’ } | ForEach-Object { $.’#text’ = ‘something’ } $xml.Save($path)

I tried fooling around with dot notation but I’m out of my depth. Thanks

submitted by /u/INTPx
[link] [comments]

The post XML: modify a child node if a sibling node has a particular value? appeared first on How to Code .NET.


Viewing all articles
Browse latest Browse all 8793

Trending Articles