Powershell 将 XML here-string 插入 xml 文档
Powershell insert XML here-string into xml document
我正在尝试将 XML 此处字符串插入到 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分别创建一个element,然后赋值request的innertext值。
$Doc = New-Object System.Xml.XmlDocument
$declaration = $Doc.CreateXmlDeclaration("1.0","UTF-8",$null)
$Doc.AppendChild($declaration)
$root = $Doc.CreateNode("element","BrowseDetailsRequest",$null)
$elUpdate = $doc.CreateElement("element","Update",$null)
$elRequest = $doc.CreateElement("element","Request",$null)
$elRequest.InnerText = "Test"
$elUpdate.AppendChild($elRequest)
$root.AppendChild($elUpdate)
$doc.AppendChild($root)
我正在尝试将 XML 此处字符串插入到 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分别创建一个element,然后赋值request的innertext值。
$Doc = New-Object System.Xml.XmlDocument
$declaration = $Doc.CreateXmlDeclaration("1.0","UTF-8",$null)
$Doc.AppendChild($declaration)
$root = $Doc.CreateNode("element","BrowseDetailsRequest",$null)
$elUpdate = $doc.CreateElement("element","Update",$null)
$elRequest = $doc.CreateElement("element","Request",$null)
$elRequest.InnerText = "Test"
$elUpdate.AppendChild($elRequest)
$root.AppendChild($elUpdate)
$doc.AppendChild($root)