Xml 从Powershell中插入具有多个属性的元素

Xml 从Powershell中插入具有多个属性的元素,xml,powershell,Xml,Powershell,我想将这一行添加到system.web标记下的xml文件中 <machineKey decryption="AES" validation="SHA1" /> 我不确定是什么原因阻止您多次调用SetAttribute(): $ChildElement.SetAttribute('decryption','False') $ChildElement.SetAttribute('validation','SHA1') 您需要使用SetAttribute()两次,每个属性一次 if($

我想将这一行添加到system.web标记下的xml文件中

<machineKey decryption="AES" validation="SHA1" />

我不确定是什么原因阻止您多次调用
SetAttribute()

$ChildElement.SetAttribute('decryption','False')
$ChildElement.SetAttribute('validation','SHA1')

您需要使用
SetAttribute()
两次,每个属性一次

if($xmldoc.configuration.'system.web'.SelectSingleNode("machineKey") -eq $null) {
    $ChildElement = $xmlDoc.CreateElement('machineKey');
    $ChildElement.SetAttribute('decryption','AES')
    $ChildElement.SetAttribute('validation','SHA1')
    $xmlDoc.configuration.'system.web'.InsertBefore($ChildElement,$xmlDoc.configuration.'system.web'.authentication)
}

$xmlDoc.Save($fileName)
这将添加此节点:

<machineKey decryption="AES" validation="SHA1" />


您是否遇到问题?如果是,那是什么呢?配置文件在从特定位置保存时遇到一些问题。作品谢谢快速问题我们如何检查特定节点是否存在,并在不存在时添加。使用foreach?
if($xmldoc.system.web)。选择singlenode(“machineKey”)-eq$null){缺少元素,添加它}
。顺便说一句。您确定不想在
授权之前添加它吗
authentication
不是xml中的元素,因此它将添加到
system.web
nodeThanks!是的,我对授权做了更改。:)
if($xmldoc.configuration.'system.web'.SelectSingleNode("machineKey") -eq $null) {
    $ChildElement = $xmlDoc.CreateElement('machineKey');
    $ChildElement.SetAttribute('decryption','AES')
    $ChildElement.SetAttribute('validation','SHA1')
    $xmlDoc.configuration.'system.web'.InsertBefore($ChildElement,$xmlDoc.configuration.'system.web'.authentication)
}

$xmlDoc.Save($fileName)
<machineKey decryption="AES" validation="SHA1" />