Merge XML based node value

Avi

I wish to merge two XML files, similar to What is the fastest way to combine two xml files into one

but I cannot wrap my head around how to group and merge them based on of the node values (Configuration node's Domain node value) I am trying it with Linq, but it doesn't make it easier, even the group by and where clause is there.

Basically I wish all Component nodes (duplicates are allowed) to be listed under the same Configuration node, which Domain name node values are equal.

In other words with the below example: the result XML has two Configuration nodes, one with Domain: MyDom01 the other is Domain: MyDom02 and under each configuration I have one Components node with all the Component listed.

Is that even possible?

One.XML

<System>
    <Configurations>
    
        <Configuration>
            <Domain>MyDom01</Domain>
            <Components>
                <Component>
                    <Name>Memory</Name>
                    <Size>16</Size>
                </Component>
                <Component>
                    <Name>CPU</Name>
                    <Size>8</Size>
                </Component>
            </Components>
        </Configuration>

        <Configuration>
            <Domain>MyDom01</Domain>
            <Components>
                <Component>
                    <Name>HDD</Name>
                    <Size>1</Size>
                </Component>
            </Components>
        </Configuration>
                    
        <Configuration>
            <Domain>MyDom02</Domain>
            <Components>
                <Component>
                    <Name>CPU</Name>
                    <Size>12</Size>
                </Component>
            </Components>
        </Configuration>

    </Configurations>
</System>

Another.XML

<System>
    <Configurations>
    
        <Configuration>
            <Domain>MyDom01</Domain>
            <Components>
                <Component>
                    <Name>Memory</Name>
                    <Size>128</Size>
                </Component>
                <Component>
                    <Name>CPU</Name>
                    <Size>32</Size>
                </Component>
                <Component>
                    <Name>CPU</Name>
                    <Size>32</Size>
                </Component>
            </Components>
        </Configuration>
                    
        <Configuration>
            <Domain>MyDom02</Domain>
            <Components>
                <Component>
                    <Name>Memory</Name>
                    <Size>32</Size>
                </Component>
            </Components>
        </Configuration>

    </Configurations>
</System>

Merged.XML:

<System>
    <Configurations>
    
        <Configuration>
            <Domain>MyDom01</Domain>
            <Components>
                <Component>
                    <Name>Memory</Name>
                    <Size>16</Size>
                </Component>
                <Component>
                    <Name>CPU</Name>
                    <Size>8</Size>
                </Component>

                <Component>
                    <Name>HDD</Name>
                    <Size>1</Size>
                </Component>

                <Component>
                    <Name>Memory</Name>
                    <Size>128</Size>
                </Component>
                <Component>
                    <Name>CPU</Name>
                    <Size>32</Size>
                </Component>
                <Component>
                    <Name>CPU</Name>
                    <Size>32</Size>
                </Component>                
            </Components>
        </Configuration>
                    
        <Configuration>
            <Domain>MyDom02</Domain>
            <Components>
                <Component>
                    <Name>CPU</Name>
                    <Size>12</Size>
                </Component>
                <Component>
                    <Name>Memory</Name>
                    <Size>32</Size>
                </Component>
            </Components>
        </Configuration>

    </Configurations>
</System>
har07

You can try to find <Components> element with matching <Domain> in XML1 then add <Component> elements from XML2 to it, and add <Configuration> from XML2 to <Configurations> in XML1 if no such matching <Domain> was found, for example:

var xdoc1 = XDocument.Load("path/to/xml1.xml");
var xdoc2 = XDocument.Load("path/to/xml2.xml");

foreach (var item in xdoc2.Descendants("Configuration"))
{
    var domain = (string)item.Element("Domain");

    // try to find <Components> with matching <Domain> in XML1
    var parent = xdoc1.Descendants("Configuration")
                        .Where(o => (string)o.Element("Domain") == domain)
                        .Elements("Components")
                        .FirstOrDefault();

    // if such <Components> is found, add <Component> from XML2 to it
    if(parent != null) 
    {
        parent.Add(item.Elements("Components").Elements("Component"));
    }
    // otherwise add <Configuration> from XML2 to <Configurations> in XML1 
    else 
    {
        xdoc1.Root.Element("Configurations").Add(item);
    }
}

// don't forget to save the modified XML1 if you need to
// here we only print the merged XML1
System.Console.WriteLine(xdoc1.ToString());

dotnetfiddle demo

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Select XML node based on value of sibling node

From Dev

xml xsl node selection based on attribute value

From Dev

Openrefine: select XML based on child node value

From Dev

Echo XML child node value based on node attribute value

From Dev

Echo XML child node value based on node attribute value

From Dev

Delete node from xml using xslt based on node value

From Dev

XML value frm few nodes based on another node value

From Dev

find element's value based on node's position in xml document

From Dev

Merge 2 XML files (including node attributes) based on attribute values using XSLT

From Dev

query to merge records based on value

From Dev

XSLT- Copy node from other XML file, based on matching node value

From Dev

accessing xml node value

From Dev

XML Split Node on . value

From Dev

XML Split Node on . value

From Dev

XML Node Value with Attribute

From Dev

I need XML SQL query to get sum of nodes value based on another node value

From Dev

I need XML SQL query to get sum of nodes value based on another node value

From Dev

select an xml descendants based on a node

From Dev

Merge Xml elements based on attribute using XSLT

From Dev

Merge Xml elements based on attribute using XSLT

From Dev

LINQ to XML - filter XElement based on its node value compared to another XElement?

From Dev

How to get specific block(group) based on child node's value in XPath from the XML?

From Dev

Sort the XML based on node value using <xsl:sort> and change the element name issue

From Dev

Select text from linked in XML file based on the value of a specific atttribute in current node

From Dev

XSLT query issues while fetching xml node value based on numeric range configured

From Dev

get value based on following node

From Dev

Access node based on attribute value

From Java

Pandas merge function based on the last value

From Dev

Python merge two arrays based on specific value

Related Related

  1. 1

    Select XML node based on value of sibling node

  2. 2

    xml xsl node selection based on attribute value

  3. 3

    Openrefine: select XML based on child node value

  4. 4

    Echo XML child node value based on node attribute value

  5. 5

    Echo XML child node value based on node attribute value

  6. 6

    Delete node from xml using xslt based on node value

  7. 7

    XML value frm few nodes based on another node value

  8. 8

    find element's value based on node's position in xml document

  9. 9

    Merge 2 XML files (including node attributes) based on attribute values using XSLT

  10. 10

    query to merge records based on value

  11. 11

    XSLT- Copy node from other XML file, based on matching node value

  12. 12

    accessing xml node value

  13. 13

    XML Split Node on . value

  14. 14

    XML Split Node on . value

  15. 15

    XML Node Value with Attribute

  16. 16

    I need XML SQL query to get sum of nodes value based on another node value

  17. 17

    I need XML SQL query to get sum of nodes value based on another node value

  18. 18

    select an xml descendants based on a node

  19. 19

    Merge Xml elements based on attribute using XSLT

  20. 20

    Merge Xml elements based on attribute using XSLT

  21. 21

    LINQ to XML - filter XElement based on its node value compared to another XElement?

  22. 22

    How to get specific block(group) based on child node's value in XPath from the XML?

  23. 23

    Sort the XML based on node value using <xsl:sort> and change the element name issue

  24. 24

    Select text from linked in XML file based on the value of a specific atttribute in current node

  25. 25

    XSLT query issues while fetching xml node value based on numeric range configured

  26. 26

    get value based on following node

  27. 27

    Access node based on attribute value

  28. 28

    Pandas merge function based on the last value

  29. 29

    Python merge two arrays based on specific value

HotTag

Archive