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")