1

If I want to remove from document lines with some string key ("foo" for example) I use this:

$content = Get-Content 'C:/fake.txt' | Where-Object {$_ -notmatch 'foo'}
$content | Out-File 'C:/fake.txt'

But now I have file with this scheme:

...
<data name="BLABLA" xml:space="preserve">
   <value>some data here</value>
</data>
...
<data name="BLABLA22" xml:space="preserve">
   <value>some data</value>
   <comment>some comment</comment>
</data>

And I need to remove for key "BLABLA" this three lines

<data name="BLABLA" xml:space="preserve">
   <value>some data here</value>
</data>

And for key "BLABLA2" this four lines

<data name="BLABLA22" xml:space="preserve">
   <value>some data</value>
   <comment>some comment</comment>
</data>

How can I do this by means of powershell?

1 Answer 1

4

If you want to delete the complete node, then the following should get you there.

# load the file into xml
[xml]$dom = gc file.xml

# find the node
$nod = $dom.SelectSingleNode("/root/data[@name='BLABLA']")

# remove the node from the parent
$nod.ParentNode.RemoveChild($nod)

# save the xml
$dom.save("file.xml")

I've assumed your data looks a bit like this:

<root>
    <data name="BLABLA" xml:space="preserve">
       <value>some data here</value>
    </data>
    <data name="BLABLA22" xml:space="preserve">
       <value>some data</value>
       <comment>some comment</comment>
    </data>
</root>
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.