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.
This topic is 3 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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
Last visit: Thu Jun 04, 2020 2:38 am

Re: XML Merge

Post by Uttam7623 »

Thank you for your help. I'm looking for something like that
User avatar
kellam
Posts: 5
Last visit: Wed Jul 15, 2020 4:50 am

Re: XML Merge

Post by kellam »

Have you tried this?
  1. $finalXml = "<root>"
  2. foreach ($file in $files) {
  3.     [xml]$xml = Get-Content $file    
  4.     $finalXml += $xml.InnerXml
  5. }
  6. $finalXml += "</root>"
  7. ([xml]$finalXml).Save("$pwd\final.xml")
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: XML Merge

Post by jvierra »

kellam wrote: Mon Jun 15, 2020 9:22 am Have you tried this?
  1. $finalXml = "<root>"
  2. foreach ($file in $files) {
  3.     [xml]$xml = Get-Content $file    
  4.     $finalXml += $xml.InnerXml
  5. }
  6. $finalXml += "</root>"
  7. ([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.
This topic is 3 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked