Madhava SM
Madhava SM

Reputation: 33

Xml merge using C#

xml1:
<value>
    <Id>1</id>
    <name>AAA</name>
</value>
<value>
    <Id>2</id>
    <name>bbb</name>
</value>
<value>
    <Id>3</id>
    <name>ccc</name>
</value>

xml2:

<value>
<Id>1</id>
<Company>abc</Company>
<address>ASD</address>
</value>

result :
    <value>
        <Id>1</id>
        <name>AAA</name>
        <Company>abc</Company>
        <address>ASD</address>
    </value>
    <value>
        <Id>2</id>
        <name>bbb</name>
    </value>
    <value>
        <Id>3</id>
        <name>ccc</name>
    </value>

if id of 1st xml = 2nd xml then merge the 2nd xml record with 1st xml value record. In my code i havnt checked id values but im juz trying to merge the 2nd xml record along with root with 1xml record. it will be great if u help me out. im getting error has The node to be inserted is from a different document context.

 XmlNode x1 = doc1.SelectSingleNode("/OutLooksync/value");
            foreach (XmlNode node in x1.SelectNodes("/OutLooksync/value"))
            {
                x1.AppendChild(doc2.ImportNode(node, true));

            }
 

Upvotes: 1

Views: 121

Answers (3)

LDS
LDS

Reputation: 362

            string xml1 = @"
    <OutLooksync>
        <value>
            <id>1</id>
            <name>AAA</name>
        </value>
        <value>
            <id>2</id>
            <name>bbb</name>
        </value>
        <value>
            <id>3</id>
            <name>ccc</name>
        </value>
    </OutLooksync>";

            string xml2 = @"
    <OutLooksync>
        <value>
            <id>4</id>
            <Company>abc</Company>
            <address>ASD</address>
        </value>
    </OutLooksync>";
            XmlDocument doc1 = new XmlDocument();
            doc1.LoadXml(xml1);

            XmlDocument doc2 = new XmlDocument();
            doc2.LoadXml(xml2);
            XmlNode newvalue = doc1.ImportNode(doc2.DocumentElement.FirstChild, true);
            doc1.DocumentElement.AppendChild(newvalue);

Upvotes: 1

Mohammed Sajid
Mohammed Sajid

Reputation: 4903

By using XmlDocument,

  • you could loop in the values in xml1 and in the values in xml2
  • Check if id equals, and import childNodes from xml2

1 - Xml for test:

string xml1 = @"
    <OutLooksync>
        <value>
            <id>1</id>
            <name>AAA</name>
        </value>
        <value>
            <id>2</id>
            <name>bbb</name>
        </value>
        <value>
            <id>3</id>
            <name>ccc</name>
        </value>
    </OutLooksync>";

string xml2 = @"
    <OutLooksync>
        <value>
            <id>1</id>
            <Company>abc</Company>
            <address>ASD</address>
        </value>
    </OutLooksync>";

2 - code for importing nodes

XmlDocument xmlDocument1 = new XmlDocument();
xmlDocument1.LoadXml(xml1);

XmlDocument xmlDocument2 = new XmlDocument();
xmlDocument2.LoadXml(xml2);

XmlNodeList values1 = xmlDocument1.SelectNodes("/OutLooksync/value");
XmlNodeList values2 = xmlDocument2.SelectNodes("/OutLooksync/value");

foreach(XmlNode value1 in values1)
{
    foreach(XmlNode value2 in values2)
    {
        if(value1.SelectSingleNode("./id").InnerText != value2.SelectSingleNode("./id")?.InnerText)
        {
            continue;
        }
        
        foreach(XmlNode toImport in value2.ChildNodes)
        {
            if (toImport.Name == "id")
                continue;

            value1.AppendChild(xmlDocument1.ImportNode(toImport, true));
        }
    }
}

Console.WriteLine(xmlDocument1.InnerXml);

I hope you find this helpful.

Upvotes: 1

LDS
LDS

Reputation: 362

XmlNodeList x1 = doc1.SelectSingleNodes("/OutLooksync/value");
    foreach (XmlNode node in x1)
        {
            doc2.appendChild(node);

         }

Upvotes: 0

Related Questions