Xml 如何在父节点为空时在Powershell中插入子节点

Xml 如何在父节点为空时在Powershell中插入子节点,xml,powershell,Xml,Powershell,由此: <products> </products> 下面给出的解决方案几乎可以工作,除了AppendChild对空值不起作用。我已经在Powershell 2上准确地输入了给定的示例 创建属性节点并将其附加到节点的属性: [xml]$xml='' $newproduct=$xml.CreateElement('product') $attr=$xml.CreateAttribute('code')) $attr.Value='id1' $newproduct.Attr

由此:

<products>
</products>
下面给出的解决方案几乎可以工作,除了AppendChild对空值不起作用。我已经在Powershell 2上准确地输入了给定的示例


创建属性节点并将其附加到
节点的属性:

[xml]$xml=''
$newproduct=$xml.CreateElement('product')
$attr=$xml.CreateAttribute('code'))
$attr.Value='id1'
$newproduct.Attributes.Append($attr)
$products=$xml.SelectSingleNode(“//products”)
$products.AppendChild($newproduct)
结果:


演示(PowerShell v2):

PS C:\>$PSVersionTable
名称值
----                           -----
CLR版本2.0.50727.3662
构建版本6.0.6002.18111
PS2.0版
WSManStack2.0版
PSCompatibleVersions{1.0,2.0}
序列化版本1.1.0.1
PSRemotingProtocol2.1版
PS C:\>[xml]$xml=''
PS C:\>$newproduct=$xml.CreateElement('product'))
PS C:\>$attr=$xml.CreateAttribute('code'))
PS C:\>$attr.Value='id1'
PS C:\>$newproduct.Attributes.Append($attr)
#正文
-----
id1
PS C:\>$products=$xml.SelectSingleNode(“//products”)
PS C:\>$products.AppendChild($newproduct)
代码
----
id1
PS C:\>$xml.Save([Console]::Out)+“`n”

创建属性节点并将其附加到
节点的属性:

[xml]$xml=''
$newproduct=$xml.CreateElement('product')
$attr=$xml.CreateAttribute('code'))
$attr.Value='id1'
$newproduct.Attributes.Append($attr)
$products=$xml.SelectSingleNode(“//products”)
$products.AppendChild($newproduct)
结果:


演示(PowerShell v2):

PS C:\>$PSVersionTable
名称值
----                           -----
CLR版本2.0.50727.3662
构建版本6.0.6002.18111
PS2.0版
WSManStack2.0版
PSCompatibleVersions{1.0,2.0}
序列化版本1.1.0.1
PSRemotingProtocol2.1版
PS C:\>[xml]$xml=''
PS C:\>$newproduct=$xml.CreateElement('product'))
PS C:\>$attr=$xml.CreateAttribute('code'))
PS C:\>$attr.Value='id1'
PS C:\>$newproduct.Attributes.Append($attr)
#正文
-----
id1
PS C:\>$products=$xml.SelectSingleNode(“//products”)
PS C:\>$products.AppendChild($newproduct)
代码
----
id1
PS C:\>$xml.Save([Console]::Out)+“`n”

创建属性节点并将其附加到
节点的属性:

[xml]$xml=''
$newproduct=$xml.CreateElement('product')
$attr=$xml.CreateAttribute('code'))
$attr.Value='id1'
$newproduct.Attributes.Append($attr)
$products=$xml.SelectSingleNode(“//products”)
$products.AppendChild($newproduct)
结果:


演示(PowerShell v2):

PS C:\>$PSVersionTable
名称值
----                           -----
CLR版本2.0.50727.3662
构建版本6.0.6002.18111
PS2.0版
WSManStack2.0版
PSCompatibleVersions{1.0,2.0}
序列化版本1.1.0.1
PSRemotingProtocol2.1版
PS C:\>[xml]$xml=''
PS C:\>$newproduct=$xml.CreateElement('product'))
PS C:\>$attr=$xml.CreateAttribute('code'))
PS C:\>$attr.Value='id1'
PS C:\>$newproduct.Attributes.Append($attr)
#正文
-----
id1
PS C:\>$products=$xml.SelectSingleNode(“//products”)
PS C:\>$products.AppendChild($newproduct)
代码
----
id1
PS C:\>$xml.Save([Console]::Out)+“`n”

创建属性节点并将其附加到
节点的属性:

[xml]$xml=''
$newproduct=$xml.CreateElement('product')
$attr=$xml.CreateAttribute('code'))
$attr.Value='id1'
$newproduct.Attributes.Append($attr)
$products=$xml.SelectSingleNode(“//products”)
$products.AppendChild($newproduct)
结果:


演示(PowerShell v2):

PS C:\>$PSVersionTable
名称值
----                           -----
CLR版本2.0.50727.3662
构建版本6.0.6002.18111
PS2.0版
WSManStack2.0版
PSCompatibleVersions{1.0,2.0}
序列化版本1.1.0.1
PSRemotingProtocol2.1版
PS C:\>[xml]$xml=''
PS C:\>$newproduct=$xml.CreateElement('product'))
PS C:\>$attr=$xml.CreateAttribute('code'))
PS C:\>$attr.Value='id1'
PS C:\>$newproduct.Attributes.Append($attr)
#正文
-----
id1
PS C:\>$products=$xml.SelectSingleNode(“//products”)
PS C:\>$products.AppendChild($newproduct)
代码
----
id1
PS C:\>$xml.Save([Console]::Out)+“`n”


这在什么方面不起作用?那个代码的结果是什么?该代码中的
$products
是什么?您是如何确定此代码不起作用的?以何种方式不起作用?那个代码的结果是什么?该代码中的
$products
是什么?您是如何确定此代码不起作用的?以何种方式不起作用?那个代码的结果是什么?该代码中的
$products
是什么?您是如何确定此代码不起作用的?以何种方式不起作用?那个代码的结果是什么?该代码中的
$products
是什么?您是如何确定此代码不起作用的?错误不能在null上追加子项value@user310291用您当前的(完整的)代码更新您的问题。实际上,我在Powershell v2上尝试了您的确切代码,但似乎得到了不同的结果。@user310291我发布的代码在Powershell v2中运行良好。这不是我编造的。如果对您无效:请提供证据。@user310291您选择了节点
,但没有将其分配给变量
$products
。您需要这样:
$products=$xml.Select
<products>
    <product code="id1">
</products>
$newproduct= $products.CreateElement("product")
$newproduct.SetAttribute("code", "");                        
$newproduct.code="id1" 
$products.AppendChild($newproduct)