Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.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_Svg - Fatal编程技术网

powershell变量中存在重复的xml文档

powershell变量中存在重复的xml文档,xml,powershell,svg,Xml,Powershell,Svg,我正在努力在powershell中复制XmlDocument变量。 该脚本创建一个新的XmlDocument,添加声明和名称空间,然后生成一些svg图形节点,并将所有内容保存为.svg文件。 到目前为止一切正常 现在我想创建两个稍有不同的.svg文件(例如,不同的颜色),并因此尝试将XmlDocument变量克隆到一个新变量中 只需按如下方式分配变量: [Xml]$exportSecondSVG = $exportSVG 显然,我只是创建了一个引用,我对$exportSecondSVG文档所做

我正在努力在powershell中复制XmlDocument变量。
该脚本创建一个新的XmlDocument,添加声明和名称空间,然后生成一些svg图形节点,并将所有内容保存为.svg文件。
到目前为止一切正常

现在我想创建两个稍有不同的.svg文件(例如,不同的颜色),并因此尝试将XmlDocument变量克隆到一个新变量中

只需按如下方式分配变量:

[Xml]$exportSecondSVG = $exportSVG
显然,我只是创建了一个引用,我对$exportSecondSVG文档所做的所有更改也将影响原始$exportSVG文档

因此,我用$exportSVG.Clone()尝试了这种方法,但也不起作用,$exportSecondSVG似乎是空的:

# Create new xml File with header
[xml]$exportSVG = New-Object System.Xml.XmlDocument

# Add xml declaration header
$decNode = $exportSVG.CreateXmlDeclaration("1.0","UTF-8",$null)
$exportSVG.AppendChild($decNode) | Out-Null

# Add namespaces to svg file
$nsmgr = New-Object Xml.XmlNamespaceManager($exportSVG.NameTable)
$nsmgr.AddNamespace('xlink','http://www.w3.org/1999/xlink');
$nsmgr.AddNamespace('rdf',"http://www.w3.org/1999/02/22-rdf-syntax-ns#");
$nsmgr.AddNamespace('cc',"http://creativecommons.org/ns#");
$nsmgr.AddNamespace('dc',"http://purl.org/dc/elements/1.1/");
$nsmgr.AddNamespace('svg',"http://www.w3.org/2000/svg");

# create root svg Node
$rootSVG = $exportSVG.CreateNode("element","svg",$null)

$exportSVG.AppendChild($rootSVG) | Out-Null

# Clone original Document
[Xml]$exportSecondSVG = $exportSVG.Clone()

# DO SOME STUFF WITH $exportSVG 
# DO DIFFERENT STUFF WITH $exportSecondSVG

$exportSVG.Save("D:\File1.xml")
$exportSecondSVG.Save("D:\File2.xml") # <-- Empty File!
#创建带有头的新xml文件
[xml]$exportSVG=New Object System.xml.XmlDocument
#添加xml声明头
$decNode=$exportSVG.CreateXmlDeclaration(“1.0”、“UTF-8”、$null)
$exportSVG.AppendChild($decNode)| Out Null
#将名称空间添加到svg文件
$nsmgr=新对象Xml.XmlNamespaceManager($exportSVG.NameTable)
$nsmgr.AddNamespace('xlink','http://www.w3.org/1999/xlink');
$nsmgr.AddNamespace('rdf',”http://www.w3.org/1999/02/22-rdf-syntax-ns#");
$nsmgr.AddNamespace('cc',”http://creativecommons.org/ns#");
$nsmgr.AddNamespace('dc',”http://purl.org/dc/elements/1.1/");
$nsmgr.AddNamespace('svg',”http://www.w3.org/2000/svg");
#创建根svg节点
$rootSVG=$exportSVG.CreateNode(“元素”、“svg”、$null)
$exportSVG.AppendChild($rootSVG)| Out Null
#克隆原始文档
[Xml]$exportSecondSVG=$exportSVG.Clone()
#用$exportSVG做一些事情
#用$exportSecondSVG做不同的事情
$exportSVG.Save(“D:\File1.xml”)

$exportSecondSVG.Save(“D:\File2.xml”)#这段代码在PowerShell 5.1上似乎适合我。您可以考虑使用<代码> $ExpVSv.CloneNode($true)< /> >以防您将要离开的子节点。非常感谢!显然,处理部分出错,.Clone()确实按预期工作。