0

I am trying to merge two xml documents and don't want to keep the duplicates. Let me mention here that I am getting these two xml documents in a string variable. I am trying the following way but getting error. I tried with XmlDocument and XDocument but didn't succeed. Getting illegal character.

I have also tried with these solution but failed.

  1. combine two xml_docs C# xmlDocument
  2. How to merge two XmlDocuments in C#
  3. Smart merging of two XML files
  4. How can I merge XML files?
  5. How to merge two XmlDocuments in C#
  6. What is the fastest way to combine two xml files into one

Please help me on this.

public class Sample
{
    public static void Main()
    {
        var xDoc1 = string.Concat("<?xml version='1.0' encoding='utf - 8'?>", 
            "<Tp>" +
            "<Txn-Profile>" +
            "<TXN_CODE>CHD</TXN_CODE>" +
            "<NO_OF_TXN>5</NO_OF_TXN>" +
            "<MAX_TXN_AMOUNT>70500</MAX_TXN_AMOUNT" +
            "><TOTAL_AMOUNT>80500</TOTAL_AMOUNT>" +
            "<TXN_DESCUI>Cash Deposit1</TXN_DESCUI>" +
            "</Txn-Profile>" +
            "</Tp>");

        var doc1 = XDocument.Load(xDoc1);


        var xDoc2 = string.Concat("<?xml version='1.0' encoding='utf - 8'?",
            "<Tp>" +
            "<Txn-Profile>" +
            "<TXN_CODE>CHD</TXN_CODE>" +
            "<NO_OF_TXN>5</NO_OF_TXN>" +
            "<MAX_TXN_AMOUNT>90000</MAX_TXN_AMOUNT>" +
            "<TOTAL_AMOUNT>210000</TOTAL_AMOUNT>" +
            "<TXN_DESCUI>Cash Deposit1</TXN_DESCUI>" +
            "</Txn-Profile>" +
            "<Txn-Profile>" +
            "<TXN_CODE>DCL</TXN_CODE>" +
            "<NO_OF_TXN>5</NO_OF_TXN>" +
            "<MAX_TXN_AMOUNT>50000</MAX_TXN_AMOUNT>" +
            "<TOTAL_AMOUNT>200000</TOTAL_AMOUNT>" +
            "<TXN_DESCUI>Deposit By Instruments (Clearing)</TXN_DESCUI>" +
            "</Txn-Profile>" +
            "</Tp>");
        var doc2 = XDocument.Load(xDoc2);

        var mergeXmlDocs = doc1.Descendants("Tp").Union(doc2.Descendants("Tp"));

        Console.WriteLine(mergeXmlDocs.ToString());

        Console.ReadLine();
    }
}
  • What do you mean by duplicates? Do all the values need to be the same or only the TXN_CODE. If TXN_CODE is the same then which one do you want to keep? – jdweng Dec 08 '20 at 11:28
  • @jdweng, I was waiting for you whole day :). As you can see TXN_CODE-->CHD, both xmls have same code but the 2nd one is updated. I might get n number of xml docs like this and need to merge with the updated xml document. Hope you understand. – Sajid Wasim Dec 08 '20 at 11:39
  • Welcome to Stack Overflow. Unfortunately, saying "but getting error", "but didn't succeed" and "but failed" doesn't tell us *anything* about what went wrong. It's the equivalent of going to a doctor and saying "I'm ill" and expecting a diagnosis without an examination. Please read https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/ and edit your question to provide *much* more information. – Jon Skeet Dec 08 '20 at 11:55
  • So updating doc2 to include the missing items from doc1 that right approach? – jdweng Dec 08 '20 at 12:47
  • @jdweng, yes that's what I want. – Sajid Wasim Dec 08 '20 at 18:41
  • @Jon Skeet, thanks for your suggestion. I have updated my question. – Sajid Wasim Dec 08 '20 at 18:43
  • That's still *far* from a clear description. Is that a compile-time error? An exception? What *exactly* is the error message? When you say the other questions didn't help, did you end up with the same problem with *all* the other approaches? This question is still very unclear. – Jon Skeet Dec 08 '20 at 19:07

1 Answers1

0

Try following :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;


namespace ConsoleApplication176
{
    class Program
    {

        static void Main(string[] args)
        {
            string xDoc1 = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
                "<Tp>" +
                "<Txn-Profile>" +
                "<TXN_CODE>CHD</TXN_CODE>" +
                "<NO_OF_TXN>5</NO_OF_TXN>" +
                "<MAX_TXN_AMOUNT>70500</MAX_TXN_AMOUNT" +
                "><TOTAL_AMOUNT>80500</TOTAL_AMOUNT>" +
                "<TXN_DESCUI>Cash Deposit1</TXN_DESCUI>" +
                "</Txn-Profile>" +
                "</Tp>";

            var doc1 = XDocument.Parse (xDoc1);


            string xDoc2 = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
                "<Tp>" +
                "<Txn-Profile>" +
                "<TXN_CODE>DCL</TXN_CODE>" +
                "<NO_OF_TXN>5</NO_OF_TXN>" +
                "<MAX_TXN_AMOUNT>50000</MAX_TXN_AMOUNT>" +
                "<TOTAL_AMOUNT>200000</TOTAL_AMOUNT>" +
                "<TXN_DESCUI>Deposit By Instruments (Clearing)</TXN_DESCUI>" +
                "</Txn-Profile>" +
                "</Tp>";
            XDocument doc2 = XDocument.Parse(xDoc2);
            List<string> txnCodes2 = doc2.Descendants("TXN_CODE").Select(x => (string)x).ToList();


            XElement txn2 = doc2.Descendants("Txn-Profile").FirstOrDefault();

            foreach (XElement txnProfilet in doc1.Descendants("Txn-Profile"))
            {
                string txnCode1 = (string)txnProfilet.Descendants("TXN_CODE").FirstOrDefault();
                if (!txnCodes2.Contains(txnCode1))
                {
                    txn2.Add(txnProfilet);
                }
            }

        }
    }

}
jdweng
  • 33,250
  • 2
  • 15
  • 20