This is my sample xml data. Please ignore if there is any syntax error or some missing xml features. The task is to comment the above section and un-comment the below section.
<?xml version="1.0" encoding="UTF-8"?>
<providersbeans xmlns="http://www.springframework.org/schema/providers"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dns="http://www.springframework.org/schema/dns"
xsi:schemaLocation="http://www.springframework.org/schema/dns/spring-dns.xsd">
<!-- COMMENT THIS SECTION -->
<dns:auth-head alias="authHead">
<dns:user-generator>
<dns:user-data>
<!-- USER AA -->
<dns:user name="AA"
password="AAAAAAAAAAAAAAAA" role="ROLE_AA" />
<!-- USER BB -->
<dns:user name="BB"
password="XXXXXXXXXXXXXXX" role="ROLE_BB" />
</dns:user-data>
</dns:user-generator>
</dns:auth-head>
<!-- UNCOMMENT THIS SECTION-->
<!--
<bean id="authHead" class="org.xx.providers">
<property name="providers">
<list>
<ref bean="AuthProvider"/>
</list>
</property>
</bean>
-->
</providersbeans>
I tried some of the solutions but couldn't get success since it has a prefix in it. One of the code that I adapted for just commenting is this:
$getxmlpath='C:Powershellsecurityfile.xml'
$xml=[xml](Get-Content $getxmlpath)
$xml.SelectNodes("//auth-head") | ForEach-Object # used with prefix as well, but didnt work
{
$var= $_;
$mycomment = $xml.CreateComment($var.OuterXml);
$var.ParentNode.ReplaceChild($mycomment , $var);
}
$xml.Save($getxmlpath);
See Question&Answers more detail:os