这里是一个PowerShell功能到评论XML节点:
function CommentXmlNode([String] $filePath, [String] $nodeXPath)
{
[xml]$xml = Get-Content -Path "$filePath"
# Find the nodes that we want to comment
$xml.SelectNodes("$nodeXPath") | ForEach-Object {
$nodeToComment = $_;
$comment = $xml.CreateComment($nodeToComment.OuterXml);
# Comment the node
$nodeToComment.ParentNode.ReplaceChild($comment, $nodeToComment);
}
# Save the file
$xml.Save("$filePath");
}
这里是一个PowerShell功能取消注释XML节点:
function UncommentXmlNode([String] $filePath, [String] $searchCriteria)
{
[xml]$xml = Get-Content -Path "$filePath"
# Find all comments on the xml file
$xml.SelectNodes("//comment()") | ForEach-Object {
# We convert the comment to an xml
$nodeToConvert = $_;
$convertedNode = $nodeToConvert.InnerText | convertto-xml
[xml]$xmlConvertedNode = $convertedNode
# Find the comment that match our search criteria
$xmlConvertedNode.SelectNodes("/descendant::*[contains(text(), '$searchCriteria')]") | ForEach-Object {
$nodeToUncomment = $_;
$strToFind = "<!--" + $nodeToUncomment.InnerText + "-->"
$strReplacement = $nodeToUncomment.InnerText
# Replace the commented string with uncommented one
$con = Get-Content "$filePath"
$con | % { $_.Replace($strToFind, $strReplacement) } | Set-Content "$filePath"
}
}
}
您可以使用它们像这样:
CommentXmlNode "D:\temp\file.xml" "YourXPath"
-
UncommentXmlNode "D:\temp\file.xml" "Some String in the xml node to Uncomment"
每种语言都有自己的评论方式。在Powershell中它是'#',C中是'''',[XML有其自己的方式](http://www.w3schools.com/xml/xml_syntax.asp) - 见下面的答案。不要认为它是“//”或“#”(无论你最习惯什么)。 – Neolisk