XML Merge

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
User avatar
sekou2331
Posts: 282
Joined: Wed Aug 15, 2012 6:07 am

Re: XML Merge

Post by sekou2331 »

Ok dont know how else to explain it or show it to you. But thank you for your advice on the iteration. Think I have an idea of how I am going to do this. Will post this my results. Thank you for the help.

jvierra
Posts: 14543
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: XML Merge

Post by jvierra »

Sorry but unless you can produce a real world example there is no way to know what to tell you to do. There is no way to generalize what you are asking. It can easily be done for those two files if we make a whole lot of limiting assumptions that would be true only for those exact files. Any changes would likely invalidate the assumptions.

With data we need to have a true representation of data that has true relationships and meaning. Random data is never easy to work with. We must have a known starting place as in all programming, science and technology.

Uttam7623
Posts: 1
Joined: Thu Jun 04, 2020 2:28 am
Contact:

Re: XML Merge

Post by Uttam7623 »

Thank you for your help. I'm looking for something like that

User avatar
kellam
Posts: 5
Joined: Tue Jun 02, 2020 8:59 am

Re: XML Merge

Post by kellam »

Have you tried this?
  1. $finalXml = "<root>"
  2.  
  3. foreach ($file in $files) {
  4.  
  5.     [xml]$xml = Get-Content $file    
  6.  
  7.     $finalXml += $xml.InnerXml
  8.  
  9. }
  10.  
  11. $finalXml += "</root>"
  12.  
  13. ([xml]$finalXml).Save("$pwd\final.xml")

jvierra
Posts: 14543
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: XML Merge

Post by jvierra »

kellam wrote:
Mon Jun 15, 2020 9:22 am
Have you tried this?
  1. $finalXml = "<root>"
  2.  
  3. foreach ($file in $files) {
  4.  
  5.     [xml]$xml = Get-Content $file    
  6.  
  7.     $finalXml += $xml.InnerXml
  8.  
  9. }
  10.  
  11. $finalXml += "</root>"
  12.  
  13. ([xml]$finalXml).Save("$pwd\final.xml")
This does NOT merge XML. It concatenates the contents of the files which is not what the word "merge" means.

The OP is asking for something that is vague and requires some kind of copying of specific elements and attributes between XML objects that are not well specified in the question. That is why I requested a real example and not a simplified and broken example that does not clarify the task well enough.

XML is highly structured data. It is not text and concatenating XML will not perform any kind of data merge operation.

Locked