Copy XML Node From One File And Append It To Another XML File

Welcome Forums General PowerShell Q&A Copy XML Node From One File And Append It To Another XML File

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
6 months, 2 weeks ago.

  • Author
    Posts
  • #101341

    Participant
    Points: 0
    Rank: Member

    Please Help out With Powershell Script for :

    1. Delete Specific “ ” node in Config-B.xml
    2. Read specific “” node from Confi-A.xml.
    3. Append in config-B.xml with step 2 out value.

    Config-A.xml

    Config-B.xml

    
    
    #################### Get Source XML  Node Value ##########################
    
    $CustomWinClientConfigXmlSource = "SourcePath\Config.xml"
    
    [xml]$SourceConfigXml = Get-Content -Path "$CustomWinClientConfigXmlSource" -Raw
    $SourceXmlNode = $SourceConfigXml | Select-Xml -XPath "//Section[@Name='APILibraries']"
    $SourceXmlOutput = Write-Output "$SourceXmlNode"
    $SourceXMLNodeValue = "$SourceXmlOutput"
    
    
    
    
    #################### Get The Target XML  Node Value And Delete It ##############
    
    $WinClientConfigFiles = "Config.xml"
    $CustomWinClientConfigXmlTarget = "TargetPath\$WinClientConfigFiles"
    
    $Path = "$CustomWinClientConfigXmlTarget"
    
    [Xml]$servicefactoryconfig = Get-Content -Path $Path -Raw
    
    $old = $servicefactoryconfig.SelectSingleNode("/Configuration/Data/Section[@Name='APILibraries']")
    
    $parent = $old.ParentNode
    
    [void] $parent.RemoveChild($old)
    
    
    ################################# Append The Target XML  With Source XML Node Value ###########
    
    Try
    
    {
    
    $newNode = [Xml] @"
    $SourceXMLNodeValue
    "@
    
    }
    
    Catch
    
    {
    
    Write-Error -Message 'Ignoring The Error Message' -ErrorAction Ignore
    
    }
    
    [void] $parent.AppendChild($servicefactoryconfig.ImportNode($newNode.DocumentElement,$true))
    
    $servicefactoryconfig.save($path)
    
    
  • #101406

    Keymaster
    Points: 1,673
    Helping HandTeam Member
    Rank: Community Hero

    Unfortunately you can't paste XML here. You'll need to post that as a Gist, and include a URL to the Gist here.

    However, although you're clearly programming in PowerShell, you're *really* using the underlying .NET Framework. You might get a better and quicker response on a site like StackOverflow.com, where there's a much larger audience of developers also working directly with .NET.

    • #101428

      Participant
      Points: 0
      Rank: Member

      Hi,

      Thanks for your suggestion. Yes , i have got the help and it worked for me.
      I will stay in touch for various post on this community.

      Thanks
      Vivek

The topic ‘Copy XML Node From One File And Append It To Another XML File’ is closed to new replies.