Is it possible to comment out a node in an XDocument?
I have following tag.
<abc key="test" value="samplevalue"></abc>
I don't have to remove the node; I just want it to be there in the XML file in commented format. I can use something like this:
$node = $xml.selectSingleNode('//abc')
#$node.OuterXml.Insert(0,"#");
$node.$xml.Save("c:\test.xml")
But if one node spreads in two lines like
<abc key="test" value="sampleValue">
</abc>
then how do I handle this case?
Comments in XML are done with <!--
and -->
. Try this:
$xml = [xml]@"
<root>
<abc key="test" value="samplevalue"></abc>
<abc key="sa" value="dsad">sda
</abc>
</root>
"@
$node = $xml.selectSingleNode('//abc')
#OuterXML is read-only, so I took an alternative route
$node.ParentNode.InnerXml = $node.ParentNode.InnerXml.Replace($node.OuterXml, $node.OuterXml.Insert(0, "<!--").Insert($node.OuterXml.Length+4, "-->"))
$xml.Save("c:\test.xml")
test.xml
<root>
<!--<abc key="test" value="samplevalue"></abc>-->
<abc key="sa" value="dsad">sda
</abc>
</root>
You can simply create a comment node, and replace your abc nodes with these comments:
$xml = [xml]@"
<root>
<abc key="test" value="samplevalue"></abc>
<abc key="sa" value="dsad">sda
</abc>
</root>
"@;
$xml.SelectNodes("//abc") | ForEach-Object {
$abc = $_;
$comment = $xml.CreateComment($abc.OuterXml);
$abc.ParentNode.ReplaceChild($comment, $abc);
}
$xml.Save(<# filename #>);
Outputs:
<root><!--<abc key="test" value="samplevalue"></abc>--><!--<abc key="sa" value="dsad">sda
</abc>--></root>
Here a powershell function to Comment an xml node:
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");
}
Here is a powershell function to Uncomment xml node:
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"
}
}
}
You can use them like this :
CommentXmlNode "D:\temp\file.xml" "YourXPath"
--
UncommentXmlNode "D:\temp\file.xml" "Some String in the xml node to Uncomment"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With