PowerShell:如果其子节点符合特定条件,则删除xml节点

时间:2014-08-08 14:38:17

标签: xml powershell

我有一个由FileZilla创建的xml文件,其中包含多个ftp服务器的连接详细信息。

XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<FileZilla3>
    <Servers>
        <Server>
            <Host>host</Host>
            <Port>1</Port>
            <Protocol>3</Protocol>
            <Type>0</Type>
            <User>SomeUser</User>
            <Pass>p455w0rd</Pass>
            <Logontype>1</Logontype>
            <TimezoneOffset>0</TimezoneOffset>
            <PasvMode>MODE_DEFAULT</PasvMode>
            <MaximumMultipleConnections>0</MaximumMultipleConnections>
            <EncodingType>Auto</EncodingType>
            <BypassProxy>0</BypassProxy>
            <Name>Server1</Name>
            <Comments>Comment</Comments>
            <LocalDir />
            <RemoteDir />
            <SyncBrowsing>0</SyncBrowsing>Server1
        </Server>
        <Server>
            <Host>host</Host>
            <Port>1</Port>
            <Protocol>3</Protocol>
            <Type>0</Type>
            <User>SomeOtherUser</User>
            <Pass>passw0rd</Pass>
            <Logontype>1</Logontype>
            <TimezoneOffset>0</TimezoneOffset>
            <PasvMode>MODE_DEFAULT</PasvMode>
            <MaximumMultipleConnections>0</MaximumMultipleConnections>
            <EncodingType>Auto</EncodingType>
            <BypassProxy>0</BypassProxy>
            <Name>Server2</Name>
            <Comments />
            <LocalDir />
            <RemoteDir />
            <SyncBrowsing>0</SyncBrowsing>Server2
        </Server>
    </Servers>
</FileZilla3>

现在我正在编写一个脚本来选择ftp帐户,然后从这个xml文件中删除。 这就是我到目前为止所拥有的:

$SiteManager = "C:\Temp\SiteManager.xml"

[XML]$SiteManagerXMLContent = Get-Content $SiteManager -Encoding UTF8

#Account that gets removed
$FTPAccName     = "Server1"
$FTPAccUserName = "SomeUser"
$FTPAccPassWord = "p455w0rd"
$FTPAccComment  = "Comment"

ForEach($Server in $SiteManagerXMLContent.FileZilla3.Servers)
{
    $XMLServerName = $Server.SelectSingleNode("//Name[.='$FTPAccName']")
    $XMLUserName = $Server.SelectSingleNode("//User[.='$FTPAccUserName']")
    $XMLPassWord = $Server.SelectSingleNode("//Pass[.='$FTPAccPassWord']")
    $XMLComment = $Server.SelectSingleNode("//Comment[.='$FTPAccComment']")

    if($XMLServerName.'#text' -eq $FTPAccName -and $XMLUserName.'#text' -eq $FTPAccUserName -and $XMLPassWord.'#text' -eq $FTPAccPassWord -and $XMLComment.'#text' -eq $FTPAccComment)
    {
        $XMLPassWord.ParentNode.RemoveAll()
    }
}

$SiteManagerXMLContent.Save($sitemanager)

这将删除所选服务器的所有子节点,但不删除父节点:这是我瞄准的目标。我想删除整个节点。

运行此脚本后,我的xml看起来像这样:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<FileZilla3>
  <Servers>
    <Server>
    </Server>
    <Server>
      <Host>host</Host>
      <Port>1</Port>
      <Protocol>3</Protocol>
      <Type>0</Type>
      <User>SomeOtherUser</User>
      <Pass>passw0rd</Pass>
      <Logontype>1</Logontype>
      <TimezoneOffset>0</TimezoneOffset>
      <PasvMode>MODE_DEFAULT</PasvMode>
      <MaximumMultipleConnections>0</MaximumMultipleConnections>
      <EncodingType>Auto</EncodingType>
      <BypassProxy>0</BypassProxy>
      <Name>Server2</Name>
      <Comments />
      <LocalDir />
      <RemoteDir />
      <SyncBrowsing>0</SyncBrowsing>Server2
        </Server>
  </Servers>
</FileZilla3>

我面临的问题是每个服务器都有相同的标签,这就是我需要通过其子节点识别正确服务器的原因。

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

您可以尝试从父母中移除父节点,如下所示:

$XMLPassWord.ParentNode.ParentNode.RemoveChild($XMLPassWord.ParentNode)