Đây là chức năng của PowerShell để Comment một nút 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");
}
Đây là một chức năng PowerShell để nút xml Bỏ ghi chú:
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"
}
}
}
Bạn có thể sử dụng chúng như thế này:
CommentXmlNode "D:\temp\file.xml" "YourXPath"
-
UncommentXmlNode "D:\temp\file.xml" "Some String in the xml node to Uncomment"
Mọi ngôn ngữ đều có cách nhận xét riêng. Trong Powershell nó là '#', trong C nó là '//', và [XML có cách riêng của nó] (http://www.w3schools.com/xml/xml_syntax.asp) - xem câu trả lời dưới đây. Đừng cho rằng đó là '//' hoặc '#' ở mọi nơi (tùy thuộc vào bạn được sử dụng nhiều nhất). – Neolisk