How to remove a part of outerxml of a node of an xml document

My XML look like this:

<?xml version="1.0"?>
<Test.New.Setting>
  <Settings>
    <Setting
Id = "1"
Line1="true"
Line2="false"
Line3="blah">
    </Setting>
  </Settings>
</Test.New.Setting>

I want to remove: Line2="false" 

Is there a way to do this with PowerShell?

September 8th, 2015 2:30pm

Your XMLL looks like this:

<?xml version="1.0"?>
 <Test.New.Setting>
   <Settings>
     <Setting Id="1" Line1="true" Line2="false" Line3="blah">
     </Setting>
   </Settings>
 </Test.New.Setting>

You can get the attributes of the "Setting" and remove any one needed.

$node=SelectSingleNode('//Setting[@Id=""])
$node.Attributes
$node.RemoveAttribute('Line1')
$node

Free Windows Admin Tool Kit Click here and download it now
September 8th, 2015 2:44pm

Thank you! Tested OK like this:

@'
<?xml version="1.0" encoding="utf-8"?>
<!--?xml version="1.0"?-->
<Test.New.Setting>
  <Settings>
    <Setting
Id = "1"
Line1="true"
Line2="false"
Line3="blah">
    </Setting>
  </Settings>
</Test.New.Setting>

'@ | Set-Content ($XMLFile = "$env:temp\test.xml")
$OutXMLFile = "$env:temp\ResultTestXML"

$xml = New-Object -TypeName XML
$xml.Load($XMLfile)
$node=$xml.SelectSingleNode('//Setting[@Id="1"]')
$node.Attributes
$node.RemoveAttribute('Line2')
$node.Attributes
$xml.Save($OutXMLFile)
Get-Content $OutXMLFile
Remove-Item $OutXMLFile -Verbose

September 9th, 2015 2:23am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics