Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.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
注释XML节(在节点中有前缀)和取消注释其他_Xml_Powershell - Fatal编程技术网

注释XML节(在节点中有前缀)和取消注释其他

注释XML节(在节点中有前缀)和取消注释其他,xml,powershell,Xml,Powershell,这是我的示例xml数据。如果存在任何语法错误或缺少某些xml功能,请忽略。 任务是对上一节进行注释,并对下一节进行取消注释 我尝试了一些解决方案,但没有成功,因为它有一个前缀。 其中一条代码是我用来评论的: $getxmlpath='C:\Powershell\securityfile.xml' $xml=[xml](Get-Content $getxmlpath) $xml.SelectNodes("//auth-head") | ForEach-Object # used with pr

这是我的示例xml数据。如果存在任何语法错误或缺少某些xml功能,请忽略。 任务是对上一节进行注释,并对下一节进行取消注释


我尝试了一些解决方案,但没有成功,因为它有一个前缀。 其中一条代码是我用来评论的:

$getxmlpath='C:\Powershell\securityfile.xml'
$xml=[xml](Get-Content $getxmlpath)
$xml.SelectNodes("//auth-head") | ForEach-Object # used with prefix as well, but didnt work
{ 
  $var= $_;
  $mycomment = $xml.CreateComment($var.OuterXml);
  $var.ParentNode.ReplaceChild($mycomment , $var);
}
$xml.Save($getxmlpath);

类似的方法可能会奏效:

[xml]$xml = Get-Content 'C:\Powershell\securityfile.xml'

# create namespace manager
$nsm = New-Object Xml.XmlNamespaceManager($xml.NameTable)
$nsm.AddNamespace('dns', $xml.DocumentElement.dns)

# remove nested comments from <auth-head> node(s)
($xml.SelectNodes('//dns:auth-head//comment()', $nsm)) | % {
  [void]$_.ParentNode.RemoveChild($_)
}
# comment-out node(s)
($xml.SelectNodes('//dns:auth-head', $nsm)) | % {
  $comment = $xml.CreateComment($_.OuterXml)
  [void]$_.ParentNode.ReplaceChild($comment, $_)
}

# uncomment <bean> node(s)
($xml.SelectNodes('//comment()')) | ? {
  $_.InnerText -like '*<bean*'
} | % {
  $newxml = [xml]$_.InnerText
  $node = $xml.ImportNode($newxml.DocumentElement, $true)
  $node.SetAttribute('xmlns', $xml.DocumentElement.NamespaceURI)
  [void]$_.ParentNode.ReplaceChild($node, $_)
}

谢谢,只需要保存文件。不管怎么说,我并不认为首先要消除所有的注释,因为xml上的注释仍然存在,它根本不起作用,至少对我来说是这样。感谢您的及时帮助again@Kriti嵌套注释会提前终止已注释掉的节点,从而留下无效的XML。我在回答中加了一条关于这一点的说明。