Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5938357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:40:27+00:00 2026-05-22T15:40:27+00:00

I have two xml files that both have the same schema and I would

  • 0

I have two xml files that both have the same schema and I would like to merge into a single xml file. Is there an easy way to do this?

For example,

<Root>
    <LeafA>
        <Item1 />
        <Item2 />
    </LeafA>
    <LeafB>
        <Item1 />
        <Item2 />
    </LeafB>
</Root>

+

<Root>
    <LeafA>
        <Item3 />
        <Item4 />
    </LeafA>
    <LeafB>
        <Item3 />
        <Item4 />
    </LeafB>
</Root>

= new file containing

<Root>
    <LeafA>
        <Item1 />
        <Item2 />
        <Item3 />
        <Item4 />
    </LeafA>
    <LeafB>
        <Item1 />
        <Item2 />
        <Item3 />
        <Item4 />
    </LeafB>
</Root>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T15:40:27+00:00Added an answer on May 22, 2026 at 3:40 pm

    “Automatic XML merge” sounds like a relatively simple requirement, but when you go into all the details, it gets complex pretty fast. Merge with c# or XSLT will be much easier for more specific task, like in the answer for EF model. Using tools to assist with a manual merge can also be an option (see this SO question).

    For the reference (and to give an idea about complexity) here’s an open-source example from Java world: XML merging made easy

    Back to the original question. There are few big gray-ish areas in task specification: when 2 elements should be considered equivalent (have same name, matching selected or all attributes, or also have same position in the parent element); how to handle situation when original or merged XML have multiple equivalent elements etc.

    The code below is assuming that

    • we only care about elements at the moment
    • elements are equivalent if element names, attribute names, and attribute values match
    • an element doesn’t have multiple attributes with the same name
    • all equivalent elements from merged document will be combined with the first equivalent element in the source XML document.

    .

    // determine which elements we consider the same
    //
    private static bool AreEquivalent(XElement a, XElement b)
    {
        if(a.Name != b.Name) return false;
        if(!a.HasAttributes && !b.HasAttributes) return true;
        if(!a.HasAttributes || !b.HasAttributes) return false;
        if(a.Attributes().Count() != b.Attributes().Count()) return false;
    
        return a.Attributes().All(attA => b.Attributes(attA.Name)
            .Count(attB => attB.Value == attA.Value) != 0);
    }
    
    // Merge "merged" document B into "source" A
    //
    private static void MergeElements(XElement parentA, XElement parentB)
    {
        // merge per-element content from parentB into parentA
        //
        foreach (XElement childB in parentB.DescendantNodes())
        {
            // merge childB with first equivalent childA
            // equivalent childB1, childB2,.. will be combined
            //
            bool isMatchFound = false;
            foreach (XElement childA in parentA.Descendants())
            {
                if (AreEquivalent(childA, childB))
                {
                    MergeElements(childA, childB);
                    isMatchFound = true;
                    break;
                }
            }
    
            // if there is no equivalent childA, add childB into parentA
            //
            if (!isMatchFound) parentA.Add(childB);
        }
    }
    

    It will produce desired result with the original XML snippets, but if input XMLs are more complex and have duplicate elements, the result will be more… interesting:

    public static void Test()
    {
        var a = XDocument.Parse(@"
        <Root>
            <LeafA>
                <Item1 />
                <Item2 />
                <SubLeaf><X/></SubLeaf>
            </LeafA>
            <LeafB>
                <Item1 />
                <Item2 />
            </LeafB>
        </Root>");
        var b = XDocument.Parse(@"
        <Root>
            <LeafB>
                <Item5 />
                <Item1 />
                <Item6 />
            </LeafB>
            <LeafA Name=""X"">
                <Item3 />
            </LeafA>
            <LeafA>
                <Item3 />
            </LeafA>
            <LeafA>
                <SubLeaf><Y/></SubLeaf>
            </LeafA>
        </Root>");
    
        MergeElements(a.Root, b.Root);
        Console.WriteLine("Merged document:\n{0}", a.Root);
    }
    

    Here’s merged document showing how equivalent elements from document B were combined together:

    <Root>
      <LeafA>
        <Item1 />
        <Item2 />
        <SubLeaf>
          <X />
          <Y />
        </SubLeaf>
        <Item3 />
      </LeafA>
      <LeafB>
        <Item1 />
        <Item2 />
        <Item5 />
        <Item6 />
      </LeafB>
      <LeafA Name="X">
        <Item3 />
      </LeafA>
    </Root>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to merge two file that have the same structure, and some data
I have two XML files. They are similar, but there are two nodes of
I have an xml file like this: <root> <item> <name>one</name> <status>good</status> </item> <item> <name>two</name>
Are there any tools to effectively compare two XML schema's? I have seen some
I have built two XML files that map the content of a given folder:
I have two XML files with two different XSD schemas and different namespaces. They
I have two applications written in Java that communicate with each other using XML
I have parsed XML using both of the following two methods... Parsing the XmlDocument
I have two Activities that both list items in a table. For the time
I need to parse xml files from two sources. Both xml files contain the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.