Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I wanted to copy one xml document object to another xml document object at specific xml element. I tried but not able to do so.

Below is the sample code:
C#
public void GenerateFooterTablixcode(XmlDocument destDoc, XmlElement reportItems)
// reportItems is the xml element belongs to destDoc xml document
{
    XmlDocument copydoc = new XmlDocument();
     copydoc.Load(@"C:\Users\Documents\Visual Studio 2010\Projects\Report Project1\RdlGeneration\XMLFile1.rdl");

    XmlNode newBook = destDoc.ImportNode(copydoc.DocumentElement, true);
    destDoc.ImportNode(copydoc.DocumentElement, true);
    destDoc.DocumentElement.AppendChild(newBook);
}


With the above code copydoc object is coping at the end of destDoc object.
where I wanted to copy at the specific element reportItems of destDoc..


Can any one please help.
Posted
Updated 1-Nov-15 19:58pm
v2
Comments
George Jonsson 2-Nov-15 2:00am    
If you add your XML data (the relevant parts) it will be easier to give a specific solution.

It depends on what you want to achieve...

Let's say you have 2 xml files. You want to join data based os some condition. Please see:
C#
//the content of xml files
string s1 = @"<persons>
<person id='1'>
<name>Arahid</name>
</person>
<person id='2'>
<name>Belleamour</name>
</person>
</persons>";

string s2 = @"<dobs>
<dob id='1'>1977-01-01</dob>
<dob id='2'>1978-11-01</dob>
</dobs>";

//create documents  
XDocument xdoc1 = XDocument.Parse(s1);
XDocument xdoc2 = XDocument.Parse(s2);

//get nodes from 1. and 2. xml based on id, create new XElement
var nodes = from a in xdoc1.Descendants("person")
    join b in xdoc2.Descendants("dob") on (int)a.Attribute("id") equals (int)b.Attribute("id")
    select new XElement ("person", new XAttribute("id", (string)a.Attribute("id")),
        new XElement("name", (string)a.Element("name")),
        new XElement("dob", (string)b.Value)
    );

//create new document
XDocument xdoc3 = new XDocument();
//add root node and nodes created above
xdoc3.Add(new XElement("persons", nodes));
//xdoc3.Save(@"FullFileName.xml");


Result:
XML
<persons>
  <person id="1">
    <name>Arahid</name>
    <dob>1977-01-01</dob>
  </person>
  <person id="2">
    <name>Belleamour</name>
    <dob>1978-11-01</dob>
  </person>
</persons>



For further information, please see: Linq To Xml[^]
 
Share this answer
 
You can try this solution . Not tested but hope will solve or help you to solve your problem
public void GenerateFooterTablixcode(string sourceXMLPATH, string targetXMLPATH,string sourceNode,string targetNode)
    {
        XmlDocument sourceXML = new XmlDocument();
        XmlDocument targetXML = new XmlDocument();
        sourceXML.Load(sourceXMLPATH);
        targetXML.Load(targetXMLPATH);
        XmlNode sXN = sourceXML.SelectSingleNode(sourceNode); // sourceNode should be the node name with depth like "Root/child1/child2/sourceNode"
        XmlNode tXN = targetXML.SelectSingleNode(targetNode); // sourceNode should be the node name with depth like "Root/child1/child2/targetNode"
        sXN.InnerXml = tXN.InnerXml;
        sourceXML.Save(sourceXMLPATH);
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900