Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于删除XML子元素的Powershell脚本_Xml_Powershell - Fatal编程技术网

用于删除XML子元素的Powershell脚本

用于删除XML子元素的Powershell脚本,xml,powershell,Xml,Powershell,尝试从XML文件中删除某些子元素时遇到问题 <Smart> <Settings> <Section name="x"> <Parameter name="a" value="true" /> <Parameter name="b" value="0" /> <Parameter nam

尝试从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>

此解决方案不使用任何花哨的XML,而是读取文件、过滤字符串并将结果输出到新文件。也许这对你有用

$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
    }
}

你很接近了-你只需要在
$xml.Smart.Settings.Section
的末尾添加
参数
-请参见下文了解如何删除所有
谢谢你,Guenther,但我真正想要的是删除整行。新文件不应该有参数name=“l”value=“EAI”,这就是它所做的(使用您提供的示例文件)将xml视为文本的危险在于您的代码变得非常脆弱-例如,如果OP的xml文档缩小到一行,您的答案将删除整个内容!对于OP的用例来说,这也许是好的,但是从长远来看,使用DOM更加健壮……感谢您的支持。这正是我所需要的。
$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
    }
}
$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");