Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.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
Powershell XML添加列_Xml_Powershell_Add - Fatal编程技术网

Powershell 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 = '&

我在使用Powershell和XML时遇到了一些问题,但不明白:(

也许你能帮我

我有一个XML对象,比如

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