Powershell 将 XML here-string 插入到 xml 文档中

问题描述

我正在尝试将 XML here-string 插入到 XML 文档中。但是,保存的 XMLdoc 显示:“System.Xml.XmlDocument”,而不是内容。我该如何解决这个问题?

[xml] $Doc = New-Object System.Xml.XmlDocument

$updateNode = [xml] "<Update>                       
                          <Request>Test</Request>
                     </Update>"

#Create XML declaration
$declaration = $Doc.CreateXmlDeclaration("1.0","UTF-8",$null)

#Append XML declaration
$Doc.AppendChild($declaration)
    
#Create root element
$root = $Doc.CreateNode("element","browseDetailsRequest",$null)

#Create node based on Here-String
$node = $Doc.CreateElement("element",$updateNode,$null)

#Append node
$root.AppendChild($node)
    
#Append root element
$Doc.AppendChild($root)   

此时输出

<?xml version="1.0" encoding="UTF-8"?>
<browseDetailsRequest>
  <System.Xml.XmlDocument />
</browseDetailsRequest>

解决方法

您并没有真正操作 xml 中的文本。使用对象来操作 xml。所以你需要为update和request创建一个元素,然后赋值request的innertext值。

arr.csv