Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.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(CSCFG)节点_Xml_Powershell_Azure_Cscfg - Fatal编程技术网

如何将元素添加到PowerShell中的空XML(CSCFG)节点

如何将元素添加到PowerShell中的空XML(CSCFG)节点,xml,powershell,azure,cscfg,Xml,Powershell,Azure,Cscfg,我有一个xml文件CSCFG文件,如下所示 XML文件: 这是我在执行此操作时遇到的异常: 方法调用失败,因为[System.String]不包含名为“AppendChild”的方法 请帮助我。这是因为Certificates属性返回一个字符串: 25> $xmlDoc.Role.Certificates.GetType().FullName System.String 由于元素是没有属性的叶元素,PowerShell将元素的表示简化为包含元素内容的字符串属性。您可以通过以下方式获得您想

我有一个xml文件CSCFG文件,如下所示

XML文件:

这是我在执行此操作时遇到的异常: 方法调用失败,因为[System.String]不包含名为“AppendChild”的方法

请帮助我。

这是因为Certificates属性返回一个字符串:

25> $xmlDoc.Role.Certificates.GetType().FullName
System.String
由于元素是没有属性的叶元素,PowerShell将元素的表示简化为包含元素内容的字符串属性。您可以通过以下方式获得您想要的:

26> [xml]$xmlDoc = @'
>>>  <Role name="Role1">
>>>     <Instances count="2" />
>>>     <ConfigurationSettings>
>>>       <Setting name="Key1" value="123456" />
>>>       <Setting name="Key2" value="1234567890" />
>>>       <Setting name="Key3" value="true" />
>>>     </ConfigurationSettings>
>>>     <Certificates>
>>>     </Certificates>
>>>   </Role>
>>> '@
27> $certElem = $xmlDoc.CreateElement('Certificate')
28> $certElem.SetAttribute('name','certificate1')
29> $certElem.SetAttribute('thumbprint','12345678')
30> $xmlDoc.SelectSingleNode('/Role/Certificates').AppendChild($certElem)
31. $xmlDoc.Save($configFile)
这是因为Certificates属性返回一个字符串:

25> $xmlDoc.Role.Certificates.GetType().FullName
System.String
由于元素是没有属性的叶元素,PowerShell将元素的表示简化为包含元素内容的字符串属性。您可以通过以下方式获得您想要的:

26> [xml]$xmlDoc = @'
>>>  <Role name="Role1">
>>>     <Instances count="2" />
>>>     <ConfigurationSettings>
>>>       <Setting name="Key1" value="123456" />
>>>       <Setting name="Key2" value="1234567890" />
>>>       <Setting name="Key3" value="true" />
>>>     </ConfigurationSettings>
>>>     <Certificates>
>>>     </Certificates>
>>>   </Role>
>>> '@
27> $certElem = $xmlDoc.CreateElement('Certificate')
28> $certElem.SetAttribute('name','certificate1')
29> $certElem.SetAttribute('thumbprint','12345678')
30> $xmlDoc.SelectSingleNode('/Role/Certificates').AppendChild($certElem)
31. $xmlDoc.Save($configFile)

谢谢Keith,但是我还有一些其他问题,因为cscfg文件有一个名称空间

下面的代码使它工作起来没有任何问题。但这是xml绑定到名称空间的时候,特别是在Azure部署中的CSCFG文件中


谢谢

谢谢Keith,不过我还有其他问题,因为cscfg文件有一个名称空间

下面的代码使它工作起来没有任何问题。但这是xml绑定到名称空间的时候,特别是在Azure部署中的CSCFG文件中


谢谢

为什么这个问题被否决了??如果读者不知道答案,它会被否决吗?我不知道为什么会被否决,也许你的标题不是问题。尝试将其改写为一个问题的形式:如何向PowerShell中的空XML节点添加元素?除此之外,你以简洁的方式向我们展示了你的尝试,从而避免了最大的问题。为什么这个问题被否决了??如果读者不知道答案,它会被否决吗?我不知道为什么会被否决,也许你的标题不是问题。尝试将其改写为一个问题的形式:如何向PowerShell中的空XML节点添加元素?除此之外,您还以简洁的方式向我们展示了您所做的尝试,从而避免了最大的问题。这正是最初问题的答案。这应该是公认的答案。。。非常感谢,@Keith,为我节省了几个小时。这正是最初问题的答案。这应该是公认的答案。。。非常感谢,@Keith,节省了我几个小时。@Keith的答案应该是被接受的,因为它完全回答了你的问题,而且因为他给了你关于你还有什么问题的线索,@Keith的答案应该是被接受的,因为它完全回答了你的问题,因为他给了你线索,你还有什么问题。。。
26> [xml]$xmlDoc = @'
>>>  <Role name="Role1">
>>>     <Instances count="2" />
>>>     <ConfigurationSettings>
>>>       <Setting name="Key1" value="123456" />
>>>       <Setting name="Key2" value="1234567890" />
>>>       <Setting name="Key3" value="true" />
>>>     </ConfigurationSettings>
>>>     <Certificates>
>>>     </Certificates>
>>>   </Role>
>>> '@
27> $certElem = $xmlDoc.CreateElement('Certificate')
28> $certElem.SetAttribute('name','certificate1')
29> $certElem.SetAttribute('thumbprint','12345678')
30> $xmlDoc.SelectSingleNode('/Role/Certificates').AppendChild($certElem)
31. $xmlDoc.Save($configFile)
$xmlDoc = [System.Xml.XmlDocument](Get-Content $configFile);
$ns = New-Object System.Xml.XmlNamespaceManager($xmlDoc.NameTable)
$ns.AddNamespace("ns", $xmlDoc.DocumentElement.NamespaceURI)

$node = $xmlDoc.SelectSingleNode("//ns:Certificates", $ns)
$newNode = $xmlDoc.CreateElement("Certificate");
$newNode.SetAttribute('name',"certificate1")
$newNode.SetAttribute('value',"12345678")
$node.AppendChild($newNode)
$xmlDoc.save($configFile)