0

I'm having a problem trying to remove some sub-elements from an XML file.

<Smart>
  <Settings>
    <Section name="x">
      <Parameter name="a" value="true" />
      <Parameter name="b" value="0" />
      <Parameter name="c" value="13873" />
      <Parameter name="d" value="true" />
      <Parameter name="e" value="EAI" />
    </Section>
    <Section name="z">
      <Parameter name="h" value="true" />
      <Parameter name="i" value="0" />
      <Parameter name="j" value="13873" />
      <Parameter name="k" value="true" />
      <Parameter name="l" value="EAI" />
    </Section>
  </Settings>
</Smart>

I want to remove the whole line "Parameter name="l" value="EAI""

Any ideas? I was trying this but it is returning nothing to me.

# Read the XML file
Write-Host "OPENING XML FILE";
LogWrite "OPENING XML FILE";
$path = "\\$computer\$FileName"
[xml] $xml = Get-Content $path

# Deleting node
$npSectionName = "x"
$xml.Smart.Settings.Section | Where-Object { $_.name -eq $npSectionName } | % { 
#Remove node
$xml.Settings.RemoveChild($_)

2 Answers 2

2

You were close - you just needed to add Parameter to the end of $xml.Smart.Settings.Section - see below for how to remove all <Parameter "name="l" value="EAI" /> nodes from the document:

$xml = @"
<Smart>
  <Settings>
    <Section name="x">
      <Parameter name="a" value="true" />
      <Parameter name="b" value="0" />
      <Parameter name="c" value="13873" />
      <Parameter name="d" value="true" />
      <Parameter name="e" value="EAI" />
    </Section>
    <Section name="z">
      <Parameter name="h" value="true" />
      <Parameter name="i" value="0" />
      <Parameter name="j" value="13873" />
      <Parameter name="k" value="true" />
      <Parameter name="l" value="EAI" />
    </Section>
  </Settings>
</Smart>
"@;

$data = [xml] $xml;

# find the nodes we want to remove
$parameters = $data.Smart.Settings.Section.Parameter `
    | where-object { ($_.name -eq "l") -and ($_.value -eq "EAI") }

# remove them
foreach( $parameter in $parameters )
{
    $null = $parameter.ParentNode.RemoveChild($parameter);
}

$data.Save("c:\temp\smart.xml");
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the support. It is exactly what I need.
0

this solution does not make use of any fancy XML but reads the file, filters the string and outputs the result to a new file. maybe this works for you?

$filePath = 'c:\tmp\smartxml.xml'
$fileContent = Get-Content -Path $filepath
$filterString = 'Parameter name="l" value="EAI"'
$newFilePath = 'c:\tmp\smartnew.xml'

foreach($line in $filecontent) {
    if($line -notmatch $filterString) {
        Out-File -FilePath $newFilePath -Append -InputObject $line
    }
}

3 Comments

Thank you, Guenther, but what I really want is to delete the whole line. The new file should not have the Parameter name="l" value="EAI"
that's what it does (using the example file you provided)
The danger with treating xml as text is your code becomes very brittle - for example if the OP's xml document ever gets minified onto one line your answer will remove the entire content! Maybe that's fine for the OP's use case, but working with the DOM is more robust in the long run...

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.