如何使用PowerShell或简单批处理文件的命令提示符取消注释XML文件中的节点。
<Settings>
<SettingNode name="Installation">
<SettingNode name="Features">
<Setting name="Features" scope="Installation">
<StringArray>
<!-- Uncomment the below line to activate the required features for the Pilot Version-->
<!--<String>PilotVersion</String>-->
<String>GlobalSearchAndReplace</String>
</StringArray>
</Setting>
</SettingNode>
</SettingNode>
</Settings>
答案 0 :(得分:0)
您可以将xml视为纯文本并使用replace cmdlet:
(gc c:\files\test.xml) -replace "<!--<String>PilotVersion</String>-->","<String>PilotVersion</String>" # |sc c:\files\test.xml if you xant to save the file
答案 1 :(得分:0)
这里是取消注释作为XPATH传递的xml元素的解决方案:
首先从Xpath父级获取所有注释,然后通过在XPath子字符串上查找匹配来识别正确的注释。
构建另一个包含$ xmlNode且没有comments标记的节点,将$ xmlNode替换为该新节点的子节点。
有点棘手,我没有找到任何其他解决方案(纯文本不是解决方案)# path to the file
$File = "c:\pathtoyourfile\example.xml"
# XPath to the element to un comment
$XPath = "/Settings/SettingNode[@name='Installation']/SettingNode[@name='Features']/Setting[@name='Features' and @scope='Installation']/StringArray/String"
# get xml file
$xmlFile = [xml](Get-Content $File)
# get parent and child path from XPath
$parentXPath = $XPath.Substring(0, $XPath.LastIndexOf('/'))
$childXPath = $XPath -replace "$parentXPath/", ''
# get comment
$xmlNode = $xmlFile.SelectNodes("$parentXPath/comment()") | ? { $_.InnerText -match "<$childXPath" }
# create node containing comment content
$tempNode = $xmlFile.CreateElement("tempNode")
$tempNode.InnerXml = $xmlNode.Value
$replaceNode = $tempNode.SelectSingleNode("/$childXPath")
# process replacement
$xmlNode.ParentNode.ReplaceChild($replaceNode, $xmlNode)
# save change
$xmlFile.Save($File)