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 7516277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:04:43+00:00 2026-05-30T01:04:43+00:00

I have a table with an Xml column in Sql. All Xml files have

  • 0

I have a table with an Xml column in Sql. All Xml files have a same schema and I want to merge some of this Xml together.

For example for X1:

 <A>
     <B>
         <C id='101'>
             <D id='102'>abcd</D>
         </C>
         <C id='103'>
             <D id='104'>zxcv</D>
         </C>
     </B>
 </A>

and X2:

 <A>
     <B>
         <C id='101'>
             <D id='102'>abcd</D>
             <D id='501'>abef</D>
         </C>
         <C id='502'>
             <D id='503'>efgh</D>
         </C>
     </B>
 </A>

X1+X2=…

 <A>
     <B>
         <C id='101'>
             <D id='102'>abcd</D>
             <D id='501'>abef</D>
         </C>
         <C id='103'>
             <D id='104'>zxcv</D>
         </C>            
         <C id='502'>
             <D id='503'>efgh</D>
         </C>
     </B>
 </A>

So which choice is the best and how:

  • XQuery in Sql
  • C# XDocument and XPath
  • …
  • 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-30T01:04:45+00:00Added an answer on May 30, 2026 at 1:04 am

    I think the best way to approach this is to write a class that merges two XDocuments using the visitor pattern, with the distinction that we are always visiting nodes from the first document in parallel with nodes from the second document.

    The overall design would be something like:

    class XmlMerger
    {
        public XDocument Merge(XDocument first, XDocument second);
    
        private XElement MergeElements(XElement first, XElement second);
    
        private XAttribute MergeAttributes(XAttribute first, XAttribute second);
    
        private XText MergeTexts(XText first, XText second);
    }
    

    A specific implementation could look like this:

    class XmlMerger
    {
        public XDocument Merge(XDocument first, XDocument second)
        {
            return new XDocument(MergeElements(first.Root, second.Root));
        }
    
        private XElement MergeElements(XElement first, XElement second)
        {
            if (first == null)
                return second;
    
            if (second == null)
                return first;
    
            if (first.Name != second.Name)
                throw new InvalidOperationException();
    
            var firstId = (string)first.Attribute("id");
            var secondId = (string)second.Attribute("id");
    
            // different ids
            if (firstId != secondId)
                throw new InvalidOperationException();
    
            var result = new XElement(first.Name);
    
            var attributeNames = first.Attributes()
                .Concat(second.Attributes())
                .Select(a => a.Name)
                .Distinct();
    
            foreach (var attributeName in attributeNames)
                result.Add(
                    MergeAttributes(
                        first.Attribute(attributeName),
                        second.Attribute(attributeName)));
    
            // text-only elements
            if (first.Nodes().OfType<XText>().Any() ||
                second.Nodes().OfType<XText>().Any())
            {
                var firstText = first.Nodes().OfType<XText>().FirstOrDefault();
                var secondText = second.Nodes().OfType<XText>().FirstOrDefault();
    
                // we're not handling mixed elements
                if (first.Nodes().Any(n => n != firstText) ||
                    second.Nodes().Any(n => n != secondText))
                    throw new InvalidOperationException();
    
                result.Add(MergeTexts(firstText, secondText));
            }
            else
            {
                var elementNames = first.Elements()
                    .Concat(second.Elements())
                    .Select(e => e.Name)
                    .Distinct();
    
                foreach (var elementName in elementNames)
                {
                    var ids = first.Elements(elementName)
                        .Concat(second.Elements(elementName))
                        .Select(e => (string)e.Attribute("id"))
                        .Distinct();
    
                    foreach (var id in ids)
                    {
                        XElement firstElement = first.Elements(elementName)
                            .SingleOrDefault(e => (string)e.Attribute("id") == id);
                        XElement secondElement = second.Elements(elementName)
                            .SingleOrDefault(e => (string)e.Attribute("id") == id);
    
                        result.Add(MergeElements(firstElement, secondElement));
                    }
                }
            }
    
            return result;
        }
    
        private XAttribute MergeAttributes(XAttribute first, XAttribute second)
        {
            if (first == null)
                return second;
    
            if (second == null)
                return first;
    
            if (first.Name != second.Name)
                throw new InvalidOperationException();
    
            if (first.Value == second.Value)
                return new XAttribute(first);
    
            // can't merge attributes with different values
            throw new InvalidOperationException();
        }
    
        private XText MergeTexts(XText first, XText second)
        {
            if (first == null)
                return second;
    
            if (second == null)
                return first;
    
            if (first.Value == second.Value)
                return new XText(first);
    
            // can't merge texts with different values
            throw new InvalidOperationException();
        }
    }
    

    If this code encounters something it can’t handle (e.g. nodes with the same id but different text; or comments), it throws an exception.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML column in a table; I want to promote a certain
I have this XML in a column in my table: <keywords> <keyword name=First Name
I have a table (on sqlserver05) with an xml column. The format of the
I have a table Blah with a PK column BlahID and an XML column
I have mysql table that has a column that stores xml as a string.
Say I have a table called xml that stores XML files in a single
I'm trying to format a table from XML. Lets say I have this line
Good afternoon to all, I have this scenario: I am using SQL Server 'BulkInsert'
I have a table that contains a column that is XML data type. I
I am selecting from a table that has an XML column using T-SQL. I

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.