XML如果存在SelectSingleNode,则删除节点

时间:2013-10-17 15:01:09

标签: xml powershell

我尝试构建一个powershell脚本,如果该节点存在,则从我的web.config文件中删除一个节点。 我有以下xml结构

<configuration>
  <configSections>
    <sectionGroup name="common">
      <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging, Version=2.1.2.0, Culture=neutral, PublicKeyToken=af08829b84f0328e" />
    </sectionGroup>

  </configSections>
</configuration>

以下代码:

[xml]$xml = Get-Content $WebAppConfigPath

 $addSectionGroupNode = $xml.SelectSingleNode("//configuration/configSections/sectionGroup/add[@name='common']")
  if ($addSectionGroupNode -ne $null)
  {
    $SectionGroupNode.RemoveChild($addSectionGroupNode)
    Write-Host "REMOVED"
  }

  $xml.Save($WebAppConfigPath)

但是,无法找到该节点,也不会删除该节点。 你能帮我帮忙吗?

谢谢

1 个答案:

答案 0 :(得分:0)

//configuration/configSections/sectionGroup/add[@name='common']

<add>内立即寻找<sectionGroup>元素的其他标准中,没有这样的元素。

您的意思是使用

/configuration/configSections/sectionGroup[@name='common']

请注意,由于<configuration>是XML文档的根元素,因此在表达式开头使用//只能减慢速度。

相关问题