Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.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
如何将新的xml节点插入到空的父节点?_Xml_Powershell - Fatal编程技术网

如何将新的xml节点插入到空的父节点?

如何将新的xml节点插入到空的父节点?,xml,powershell,Xml,Powershell,这是我的xml文件结构:bookshelf.xml <?xml version="1.0"?> <Root> <bookshelf> </bookshelf> </Root> 我在运行时遇到以下异常 Method invocation failed because [System.String] does not contain a method named 'PrependChild'. At powershell.ps1:

这是我的xml文件结构:bookshelf.xml

<?xml version="1.0"?>
<Root>
  <bookshelf>
  </bookshelf>
</Root>
我在运行时遇到以下异常

Method invocation failed because [System.String] does not contain a method named 'PrependChild'.
At powershell.ps1:7 char:1
+ $bookshelf.PrependChild($bookNode);
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound
<>我尝试调试并检查选中的XML节点是空的,PosithBar不认为它是XML节点和异常抛出。在空节点中添加额外节点后,它将正常工作:

  <bookshelf>
    <test> testdata <test>
  </bookshelf>

但我不喜欢这种情况,它应该能够将新节点插入到空标记中。以前是否有人遇到过此问题?

PowerShell为xml文档提供了一些帮助功能-您的$bookshelf=$myXml.Root.bookshelf返回的是bookshelf元素的内部文本,而不是元素本身

如果您这样做,它将返回元素

$bookshelf=$myXml.SelectSingleNode/Root/bookshelf 接下来的示例将创建一个xml文档,如下所示:

第一册
如果您尝试$bookshelf=$myXml.SelectSingleNode//Root/bookshelf,然后运行代码会怎么样?除非您在[environment]::currentdirectory中,否则您的.Save命令也可能会出现问题。谢谢,它可以工作!我没想到它会返回元素而不是内部文本。
  <bookshelf>
    <test> testdata <test>
  </bookshelf>