Xml 为什么SelectSingleNode返回空对象?
我的Xml 为什么SelectSingleNode返回空对象?,xml,powershell,Xml,Powershell,我的$metas为空,因此AppendChild失败。为什么SelectSingleNode在我的代码中返回空对象: $xml=[xml]@' <?xml version="1.0" encoding="iso-8859-1"?> <catalogue> <products> <product id="pdt1"> <metas> </metas> </product>
$metas
为空,因此AppendChild
失败。为什么SelectSingleNode
在我的代码中返回空对象:
$xml=[xml]@'
<?xml version="1.0" encoding="iso-8859-1"?>
<catalogue>
<products>
<product id="pdt1">
<metas>
</metas>
</product>
<product id="pdt2">
<metas>
</metas>
</product>
</products>
</catalogue>
'@
$product_code = "pdt2"
$metas = $xml.SelectSingleNode("//catalogue/products/product[@code='$product_code']/metas")
$attr=$xml.CreateAttribute("date");
$attr.Value = "2015.07.24"
$metas.Attributes.Append($attr)
$newmeta1 = $xml.CreateElement('meta')
$attr1=$xml.CreateAttribute("code");
$attr1.Value = "123456"
$newmeta1.Attributes.Append($attr1)
$metas.AppendChild($newmeta1)
$xml=[xml]@'
'@
$product\U code=“pdt2”
$metas=$xml.SelectSingleNode(//catalog/products/product[@code='$product\u code']/metas)
$attr=$xml.CreateAttribute(“日期”);
$attr.Value=“2015.07.24”
$metas.Attributes.Append($attr)
$newmeta1=$xml.CreateElement('meta')
$attr1=$xml.CreateAttribute(“代码”);
$attr1.Value=“123456”
$newmeta1.Attributes.Append($attr1)
$metas.AppendChild($newmeta1)
您的产品
XML节点没有code
属性。相反,它有一个id
属性。因此,您应该使用[@id=…]
而不是[@code=…]
试试这个:
$metas = $xml.SelectSingleNode("//catalogue/products/product[@id='$product_code']/metas")