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
使用powershell有条件地删除xml中的child_Xml_Powershell - Fatal编程技术网

使用powershell有条件地删除xml中的child

使用powershell有条件地删除xml中的child,xml,powershell,Xml,Powershell,我有一个xml配置文件: <?xml version="1.0"?> <Tenant> <Tenants> <Tenant name="tenant_0" siteName="t100001" urlWebSite="qa.local" username="admin" password="admin" />

我有一个
xml
配置文件:

<?xml version="1.0"?>
<Tenant>
  <Tenants>
    <Tenant name="tenant_0" siteName="t100001" urlWebSite="qa.local" username="admin" password="admin" />
    <Tenant name="tenant_1" siteName="t100002" urlWebSite="qa.local" username="admin" password="admin" />
    <Tenant name="tenant_2" siteName="t100003" urlWebSite="qa.local" username="admin" password="admin" />
    <Tenant name="tenant_3" siteName="t100004" urlWebSite="qa.local" username="admin" password="admin" />
  </Tenants>
<Tenant>

如果
childnode
具有
siteName
属性,而该属性不在
$siteName
数组中,我还需要从
租户中删除
childnode

显示的xml无效,因为最后的
应该是一个结束标记:

解决了这个问题后,下面是修改后的代码:

# Best use below method to load an XML from file, because it automatically detects the file encoding
$config = New-Object System.XML.XMLDocument
$config.Load("path\xml.config")

$urlWebSite = 'prod.local'
$siteNames  = 't100001','t100002','t100003'  # don't need '@()' here. The comma is enough to create an array

foreach ($node in $config.Tenant.Tenants.Tenant) {
    if ($siteNames -contains $node.siteName) {
        # reset the attributes value
        $node.SetAttribute('urlWebSite', $urlWebSite)
    }
    else {
        # not in the $siteNames array, so remove this node
        $null = $node.ParentNode.RemoveChild($node)
    }
}

$config.save("path\xml.config")
输出:

<?xml version="1.0"?>
<Tenant>
  <Tenants>
    <Tenant name="tenant_0" siteName="t100001" urlWebSite="prod.local" username="admin" password="admin" />
    <Tenant name="tenant_1" siteName="t100002" urlWebSite="prod.local" username="admin" password="admin" />
    <Tenant name="tenant_2" siteName="t100003" urlWebSite="prod.local" username="admin" password="admin" />
  </Tenants>
</Tenant>

您显示的xml无效,因为最后的
应该是结束标记:

解决了这个问题后,下面是修改后的代码:

# Best use below method to load an XML from file, because it automatically detects the file encoding
$config = New-Object System.XML.XMLDocument
$config.Load("path\xml.config")

$urlWebSite = 'prod.local'
$siteNames  = 't100001','t100002','t100003'  # don't need '@()' here. The comma is enough to create an array

foreach ($node in $config.Tenant.Tenants.Tenant) {
    if ($siteNames -contains $node.siteName) {
        # reset the attributes value
        $node.SetAttribute('urlWebSite', $urlWebSite)
    }
    else {
        # not in the $siteNames array, so remove this node
        $null = $node.ParentNode.RemoveChild($node)
    }
}

$config.save("path\xml.config")
输出:

<?xml version="1.0"?>
<Tenant>
  <Tenants>
    <Tenant name="tenant_0" siteName="t100001" urlWebSite="prod.local" username="admin" password="admin" />
    <Tenant name="tenant_1" siteName="t100002" urlWebSite="prod.local" username="admin" password="admin" />
    <Tenant name="tenant_2" siteName="t100003" urlWebSite="prod.local" username="admin" password="admin" />
  </Tenants>
</Tenant>


为什么不使用内置的Powershell Xml cmdlet或Xml.Net名称空间来解析所需的Xml?为什么不使用内置的Powershell Xml cmdlet或Xml.Net名称空间来解析所需的Xml?