【问题标题】:Removing XML Node In PowerShell Removes Extra Tags That I Need在 PowerShell 中删除 XML 节点会删除我需要的额外标签
【发布时间】:2017-05-10 14:53:40
【问题描述】:

我有一个格式如下的 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<factory>
  <map>
    <add key="Audit" assembly="Python.Platform.Core.dll" class="Python.Core.Audit.Services.Audit">
      <parameter name="configFilePath" class="string" value="Config\Audit\AuditConfiguration.xml" />
    </add>
    <add key="Security.Password" assembly="OpenContent.Platform.Core.dll" class="OpenContent.Core.Security.Services.PasswordSecurityMD5">
    </add>
    <add key="Security.Password.Local" assembly="OpenContent.Platform.Core.dll" class="OpenContent.Core.Security.Services.PasswordSecurityMD5">
    </add>
    <add key="Guest.Registration" assembly="OpenContent.Platform.Core.dll" class="OpenContent.Core.Guest.Services.DbGuestRegistration">
      <parameter name="registrationStoredProcedure" class="string" value="AddNewGuest" />
      <parameter name="updateEncodedKeyStoredProcedure" class="string" value="UpdateGuestEncodedKey" />
      <parameter name="unregistrationStoredProcedure" class="string" value="DeleteGuest" />
      <parameter name="DeleteExpiredGuestStoredProcedure" class="string" value="DeleteExpiredGuest" />
      <parameter name="defaultValidDays" class="int" value="30" />
    </add>
  </map>
</factory>

我的目标是删除此部分:

    <add key="Audit" assembly="Python.Platform.Core.dll" class="Python.Core.Audit.Services.Audit">
      <parameter name="configFilePath" class="string" value="Config\Audit\AuditConfiguration.xml" />
    </add>

我正在运行的 PowerShell 脚本如下:

$path = "C:\testing\myxml.xml"

[Xml]$myxml = Get-Content -Path $path -Raw

$old = $myxml.SelectSingleNode("/factory/map/add[@key='Audit']")

$parent = $old.ParentNode
[void] $parent.RemoveChild($old)

$myxml.save($path)

不幸的是,当我运行它时,它还会从任何没有参数子节点的 Add 节点中删除任何结束标记。因此,与其只是删除我要删除的部分(它正在这样做),它现在看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<factory>
  <map>



    <add key="Security.Password" assembly="OpenContent.Platform.Core.dll" class="OpenContent.Core.Security.Services.PasswordSecurityMD5">

    <add key="Security.Password.Local" assembly="OpenContent.Platform.Core.dll" class="OpenContent.Core.Security.Services.PasswordSecurityMD5">

    <add key="Guest.Registration" assembly="OpenContent.Platform.Core.dll" class="OpenContent.Core.Guest.Services.DbGuestRegistration">
      <parameter name="registrationStoredProcedure" class="string" value="AddNewGuest" />
      <parameter name="updateEncodedKeyStoredProcedure" class="string" value="UpdateGuestEncodedKey" />
      <parameter name="unregistrationStoredProcedure" class="string" value="DeleteGuest" />
      <parameter name="DeleteExpiredGuestStoredProcedure" class="string" value="DeleteExpiredGuest" />
      <parameter name="defaultValidDays" class="int" value="30" />
    </add>
  </map>
</factory>

关于如何在不删除额外结束标签的情况下运行它的任何建议?

【问题讨论】:

    标签: xml powershell tags removechild


    【解决方案1】:

    这似乎是 XmlElement.WriteTo() 和类似方法的默认行为 - 对没有内部非 xml 内容的任何叶元素使用短格式标签。

    这是由每个元素的inspecting the IsEmpty property 确定的,因此您可以通过在调用Save() 之前将IsEmpty 设置为$false 来欺骗作者保留长格式标签:

    # Locate all <add /> nodes with no child elements
    $addLeafNodes = $myxml.SelectNodes('//add[not(child::*)]')
    
    # Loop through them
    foreach($node in $AddLeafNodes){
        # Set IsEmpty property to false
        $node.IsEmpty = $false
    }
    
    # Save document
    $myxml.Save($path)
    

    【讨论】:

    • 嗯...这似乎没有改变任何东西。
    • 经过进一步调查,我认为没有任何问题...我首先制作了名为 myxml.xml.original 的文件的备份副本。进行更改后,我将 myxml.xml.original 与 myxml.xml 进行了比较...显然是 CompareIt 不喜欢该行...当我查看文件时,标签肯定存在,但 CompareIt 显然没有像 xml 文件。
    • @ChristopherCass 可能是因为空格的差异
    猜你喜欢
    • 2011-03-08
    • 1970-01-01
    • 2021-09-11
    • 2021-05-31
    • 2019-10-27
    • 2010-10-23
    • 1970-01-01
    • 2019-10-03
    • 1970-01-01
    相关资源
    最近更新 更多