The task is to remove a node based on either key or value, for e.g the whole entry with key "org.quartz.B" and value="BBB" should be removed.
<config>
<module>
<section name="java">
<entry key="org.quartz.A" value="AAA" />
<entry key="org.quartz.B" value="BBB" />
<entry key="org.quartz.C" value="CCC" />
<entry key="org.quartz.D" value="false" />
</section>
<section name="db">
<entry key="oracle" value="12" />
<entry key="mssql" value="2012" />
<entry key="mysql" value="6.1" />
</section>
</module>
</config>
Code which i have tried is
$getxmlpath="C:\test.xml"
$xml=[xml](Get-Content $getxmlpath)
Write-Host ($xml)
$javasection = $xml.config.module | where-Object {$_.name -eq 'java'}
Write-Host ($javasection)
####### dont know how to delete particular node#######
$xml.Save($getxmlpath)
You can use the RemoveChild method of the XmlNode class:
$childToRemove = $javaSection.entry | ? { $_.Key -eq 'org.quartz.B' }
$javaSection.RemoveChild($childToRemove)
Note that using this to find the element to delete is inefficient as it has to filter through ALL of the elements with the Where-Object cmdlet. If your xml is going to be large, you might want to look at using XPath expressions with a method like: SelectSingleNode.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments