Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Removing the roots of a subtree in an Xml file using powershell

$
0
0

Hi all

I would like to remove a subtree in a xml file. I found how to remove the subtree, but not the root of the subtree
Here I want to remove to second <Disk> ... </Disk> data.

RemoveAll() removes everything but <Disk></Disk> tags.

How to remove theses ?

Thanks a lot !

ML

$Xml = [xml] @"
<?xml version="1.0" encoding="utf-8"?>
<unattend xmlns="urn:schemas-microsoft-com:unattend">
    <settings pass="specialize">
        <component name="Microsoft-Windows-International-Core" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                <DiskConfiguration>
                <Disk wcm:action="add">
                    <CreatePartitions>
                    </CreatePartitions>
                    <ModifyPartitions>
                    </ModifyPartitions>
                    <DiskID>0</DiskID>
                    <WillWipeDisk>true</WillWipeDisk>
                </Disk>
                <WillShowUI>OnError</WillShowUI>
               <Disk wcm:action="add">
                    <CreatePartitions>
                    </CreatePartitions>
                    <ModifyPartitions>
                    </ModifyPartitions>
                    <DiskID>1</DiskID>
                    <WillWipeDisk>true</WillWipeDisk>
                </Disk>

            </DiskConfiguration>
</component>
</settings>
            </unattend>

"@
$Xml.unattend.settings.component.DiskConfiguration

$Xml.unattend.settings.component.DiskConfiguration.disk[1].removeall()
$Xml.save(".\foo.xml")

PS C:\> type .\foo.xml
<?xml version="1.0" encoding="utf-8"?>
<unattend xmlns="urn:schemas-microsoft-com:unattend">
  <settings pass="specialize">
    <component name="Microsoft-Windows-International-Core" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas
.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <DiskConfiguration>
        <Disk wcm:action="add">
          <CreatePartitions>
          </CreatePartitions>
          <ModifyPartitions>
          </ModifyPartitions>
          <DiskID>0</DiskID>
          <WillWipeDisk>true</WillWipeDisk>
        </Disk>
        <WillShowUI>OnError</WillShowUI>
        <Disk>
        </Disk>

     </DiskConfiguration>
    </component>
  </settings>
</unattend>


ML



Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>