MKANET
MKANET

Reputation: 653

How to add an XML child element to an existing XML file?

Considering..

Orig.xml

<LayoutModificationTemplate xmlns:defaultlayout="http://schemas.microsoft.com/Start/2014/FullDefaultLayout" xmlns:start="http://schemas.microsoft.com/Start/2014/StartLayout" Version="1" xmlns="http://schemas.microsoft.com/Start/2014/LayoutModification">
  <LayoutOptions StartTileGroupCellWidth="6" />
  <DefaultLayoutOverride>
    <StartLayoutCollection>
      <defaultlayout:StartLayout GroupCellWidth="6">
        <start:Group Name="COM">
          <start:DesktopApplicationTile Size="2x2" Column="0" Row="0" DesktopApplicationLinkPath="%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\Open All COM Sessions.lnk" />
          <start:DesktopApplicationTile Size="2x2" Column="2" Row="0" DesktopApplicationLinkPath="%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\Close all COM Sessions.lnk" />
        </start:Group>
        <start:Group Name="Office">
          <start:DesktopApplicationTile Size="2x2" Column="2" Row="0" DesktopApplicationLinkPath="%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\PowerPoint 2016.lnk" />
        </start:Group>
      </defaultlayout:StartLayout>
    </StartLayoutCollection>
  </DefaultLayoutOverride>
</LayoutModificationTemplate>

StartMenuLayout-MOD.xml

<LayoutModificationTemplate xmlns:defaultlayout="http://schemas.microsoft.com/Start/2014/FullDefaultLayout" xmlns:start="http://schemas.microsoft.com/Start/2014/StartLayout" Version="1" xmlns="http://schemas.microsoft.com/Start/2014/LayoutModification">
  <LayoutOptions StartTileGroupCellWidth="6" />
  <DefaultLayoutOverride>
    <StartLayoutCollection>
      <defaultlayout:StartLayout GroupCellWidth="6">
        <start:Group Name="My-Tools">
          <start:DesktopApplicationTile Size="2x2" Column="0" Row="0" DesktopApplicationID="Microsoft.AutoGenerated.{20529041-3E03-352B-AC38-58B1C8A14CBF}" />
        </start:Group>
        <start:Group Name="COM">
          <start:DesktopApplicationTile Size="2x2" Column="0" Row="0" DesktopApplicationLinkPath="%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\Open All COM Sessions.lnk" />
          <start:DesktopApplicationTile Size="2x2" Column="2" Row="0" DesktopApplicationLinkPath="%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\Close all COM Sessions.lnk" />
        </start:Group>
        <start:Group Name="Office">
          <start:DesktopApplicationTile Size="2x2" Column="2" Row="0" DesktopApplicationLinkPath="%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\PowerPoint 2016.lnk" />
        </start:Group>
      </defaultlayout:StartLayout>
    </StartLayoutCollection>
  </DefaultLayoutOverride>
</LayoutModificationTemplate>

I would like to programmatically add the My-Tools section (shown in StartMenuLayout-MOD.xml) to Orig.xml so that both files end up identical.

Below, is what I have so far; which is probably wrong:

[xml]$xmlOrig = Get-Content C:\Orig.xml
[xml]$xmlDoc = Get-Content C:\StartMenuLayout-MOD.xml

$child = $xmlDoc.LayoutModificationTemplate.DefaultLayoutOverride.StartLayoutCollection.StartLayout.group | ?{$_.Name -eq "My-Tools"}

Ultimately, I would like to save the contents of $child to my script.. so, I can add it to an existing XML file that's structured similarly without needing StartMenuLayout-MOD.xml.

Upvotes: 0

Views: 175

Answers (1)

Doug Maurer
Doug Maurer

Reputation: 8858

Working off your existing code, you have to import the node and then prepend it.

[xml]$xmlOrig = Get-Content C:\Orig.xml
[xml]$xmlDoc = Get-Content C:\StartMenuLayout-MOD.xml

$child = $xmlDoc.LayoutModificationTemplate.DefaultLayoutOverride.StartLayoutCollection.StartLayout.group | ?{$_.Name -eq "My-Tools"}

$newnode = $xmlOrig.ImportNode($child,$true)

$xmlOrig.LayoutModificationTemplate.DefaultLayoutOverride.StartLayoutCollection.StartLayout.prependchild($newnode)

$xmlOrig.save('C:\Orig.xml')

You could just add this in your script so you don't have to pull from a file

[xml]$xmlDoc = @'
<LayoutModificationTemplate xmlns:defaultlayout="http://schemas.microsoft.com/Start/2014/FullDefaultLayout" xmlns:start="http://schemas.microsoft.com/Start/2014/StartLayout" Version="1" xmlns="http://schemas.microsoft.com/Start/2014/LayoutModification">
  <LayoutOptions StartTileGroupCellWidth="6" />
  <DefaultLayoutOverride>
    <StartLayoutCollection>
      <defaultlayout:StartLayout GroupCellWidth="6">
        <start:Group Name="My-Tools">
          <start:DesktopApplicationTile Size="2x2" Column="0" Row="0" DesktopApplicationID="Microsoft.AutoGenerated.{20529041-3E03-352B-AC38-58B1C8A14CBF}" />
        </start:Group>
        <start:Group Name="COM">
          <start:DesktopApplicationTile Size="2x2" Column="0" Row="0" DesktopApplicationLinkPath="%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\Open All COM Sessions.lnk" />
          <start:DesktopApplicationTile Size="2x2" Column="2" Row="0" DesktopApplicationLinkPath="%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\Close all COM Sessions.lnk" />
        </start:Group>
        <start:Group Name="Office">
          <start:DesktopApplicationTile Size="2x2" Column="2" Row="0" DesktopApplicationLinkPath="%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\PowerPoint 2016.lnk" />
        </start:Group>
      </defaultlayout:StartLayout>
    </StartLayoutCollection>
  </DefaultLayoutOverride>
</LayoutModificationTemplate>
'@

This is equivalent to the Get-Content line.

Upvotes: 2

Related Questions