1

I have 2 XML file named ConfigA.config(Source File) and ConfigB.config(Destination File). XML Files as follows

Source File :- ConfigA.config

<?xml version="1.0" encoding="utf-16"?>
<Configuration>
  <Data>
    <Section  EntityID="GUID1">
       <Item Name="Name_Value" Layer="Some_Layer" Type="Custom" RootNamespace="Some.API.User" 
       FileName="SomeFileName" />
    </Section >
    <Section  EntityID="GUID2">
      <Item Name="Name_Value1" Layer="Some_Layer1" Type="Custom1" RootNamespace="Some.API.User1" 
      FileName="SomeFileName1" />
    </Section >
 </Data>
</Configuration>

Destination File :- ConfigB.config

<?xml version="1.0" encoding="utf-16"?>
<Configuration>
  <Data>
      <Section EntityID="GUID1">
        <Item Name="Name_Value2" Layer="Some_Layer2" Type="Custom2" RootNamespace="Some.API.User2" 
        FileName="SomeFileName2" />
      </Section>
  </Data>
</Configuration>

I need to do following

  • Check if node exist in destination file using EntityId attribute?
  • If node exist then delete node from destination file and append node from source file to destination file
  • If node does not exist append node from source file to destination file.

What I have tried :-

I have written following powershell script which iterate both the files node and check if node exist in destination file if node exist it will removed and append node from source file. Script as follows

[xml] $serverFile = Get-Content   -Path 'C:\demo\server\ConfigB.config'
[xml] $localFile = Get-Content -Path 'C:\demo\local\ConfigA.config'

 foreach($i in $localFile.Configuration.Data.Section) { 
    $serverFile.Configuration.Data.Section | 
         ? { $_.EntityID -eq $i.EntityID } | 
         % { 
              $entityId = $_.EntityID
              Write-Output $entityId
              $SourceXmlNode = $localFile | Select-Xml -XPath "//Section[@EntityID = '$entityId']"
              $parent = $_.ParentNode
              [void]$_.ParentNode.RemoveChild($_)
              [void] $parent.AppendChild($serverFile.ImportNode($SourceXmlNode.Node, $true))
           }
  }

 $serverFile.Save('C:\demo\server\ConfigB.config')

Following stack-overflow already question-answer help me to removed and append node stuff.

Copy XML node from one file and append it to another XML file.

Problem :-
Now, With the help of above code I can removed and append node if that node exist in destination. It's working fine. But I am struggling with add new node if it is not present in destination file.

Question :-

  1. Is there any way to add else part so, that I can add logic related to add new node from source file to destination file?
  2. If there is no way to add else part. how can I change above script so, I can check if-else condition?
  3. Is there any better way to achieve this?
Harish
  • 789
  • 1
  • 7
  • 21

1 Answers1

0

So you always want to copy the node from source (local) to destination (server) and replace if the node exists, correct? Then this should work:

$parent = $serverFile.SelectSingleNode("/Configuration/Data")
# enumerate source nodes
foreach ($localNode in $localFile.SelectNodes("/Configuration/Data/Section")) {
    # check if node exists in destination
    $serverNode = $parent.SelectSingleNode("Section[@EntityID='$($localNode.EntityId)']")
    # remove if exists
    if ($serverNode) {
        [void]$parent.RemoveChild($serverNode)
    }
    # append from source
    [void]$parent.AppendChild($serverFile.ImportNode($localNode, $true))
}
marsze
  • 15,079
  • 5
  • 45
  • 61