Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I comment out a node in XML using PowerShell?

Tags:

powershell

xml

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?

like image 777
user1595214 Avatar asked May 29 '13 17:05

user1595214


3 Answers

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>
like image 129
Frode F. Avatar answered Oct 20 '22 18:10

Frode F.


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>
like image 17
Marcus Avatar answered Oct 20 '22 18:10

Marcus


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"
like image 6
Anas Avatar answered Oct 20 '22 17:10

Anas