使用批处理文件或 powershell 取消注释 xml 节点
Uncomment xml node using batch file or powershell
如何使用带有 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>
您可以将 xml 视为纯文本并使用替换 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
这是取消注释作为 XPATH 传递的 xml 元素的解决方案:
首先从 Xpath 父级获取所有注释,然后通过在 XPath 子字符串上查找匹配来识别正确的注释。
构建另一个包含该 $xml 节点但不带注释标记的节点,将 $xml 节点替换为该新节点的子节点。
有点棘手,我没有找到任何其他解决方案(纯文本不是解决方案)
# 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)
如何使用带有 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>
您可以将 xml 视为纯文本并使用替换 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
这是取消注释作为 XPATH 传递的 xml 元素的解决方案:
首先从 Xpath 父级获取所有注释,然后通过在 XPath 子字符串上查找匹配来识别正确的注释。
构建另一个包含该 $xml 节点但不带注释标记的节点,将 $xml 节点替换为该新节点的子节点。
有点棘手,我没有找到任何其他解决方案(纯文本不是解决方案)
# 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)