Powershell XML添加列
我在使用Powershell和XML时遇到了一些问题,但不明白:( 也许你能帮我 我有一个XML对象,比如Powershell XML添加列,xml,powershell,add,Xml,Powershell,Add,我在使用Powershell和XML时遇到了一些问题,但不明白:( 也许你能帮我 我有一个XML对象,比如 [xml] $a = '<test><red>1</red><blue>2</blue></test>' [xml]$a='12' 现在我想在$a中添加另一个元素,以获得如下解决方案 [xml]$solution='123' 我试着生成第二个xml对象并将其发送到第一个对象,但它不起作用 [xml] $a = '&
[xml] $a = '<test><red>1</red><blue>2</blue></test>'
[xml]$a='12'
现在我想在$a中添加另一个元素,以获得如下解决方案
[xml]$solution='123'
我试着生成第二个xml对象并将其发送到第一个对象,但它不起作用
[xml] $a = '<test><red>1</red><blue>2</blue></test>'
[xml] $b = '<test><yellow>2</yellow></test>'
($a.test).appendchild($b.test,$true)
[xml]$a='12'
[xml]$b='2'
($a.test).appendchild($b.test,$true)
你对我有什么想法吗
非常感谢,
顺致敬意,
保罗也许有一个更简单的方法,但这是可行的:
[xml] $a = '<test><red>1</red><blue>2</blue></test>'
[xml] $b = '<test><yellow>2</yellow></test>'
$b.test.ChildNodes | Foreach {
$newElem = $a.CreateElement($_.Name, $_.NamespaceURI)
$newElem.InnerXml = $_.InnerXml
$a.test.AppendChild($newElem)}
[xml]$a='12'
[xml]$b='2'
$b.test.ChildNodes | Foreach{
$newElem=$a.CreateElement($\.Name,$\.NamespaceURI)
$newElem.InnerXml=$\ InnerXml
$a.test.AppendChild($newElem)}
您需要创建一个新节点,向其追加一个文本节点,然后将其追加到现有XML中:
[xml]$a = '<test><red>1</red><blue>2</blue></test>'
$node = $a.CreateNode('element', 'yellow', '')
$text = $a.CreateTextNode(2)
$node.AppendChild($text)
$a.SelectSingleNode('/test').AppendChild($node)
[xml]$a='12'
$node=$a.CreateNode('element','yellow','')
$text=$a.CreateTextNode(2)
$node.AppendChild($text)
$a.SelectSingleNode('/test')。AppendChild($node)