c# xml import file

I'm trying to find an easy way of merging two XML files rather than re-writing the second file, one is a template and the other contains data, here is a simplified version of the xml

template.xml
<?xml version="1.0"?>
<import>
  <doctype>blah</doctype>
  <style_sheet>blah</style_sheet>
<imported_data></imported_data>
</import>

Open in new window


data.xml
<data>
 <products>
  <product>
		<code>abc</code>
		<qty>1</qty>
  </product>
  <product>
		<code>abc</code>
		<qty>1</qty>
  </product>
 </products>
</data>

Open in new window


desired output
<import>
  <doctype>blah</doctype>
  <style_sheet>blah</style_sheet>
     <imported_data>
	<data>
	 <products>
	  <product>
			<code>abc</code>
			<qty>1</qty>
	  </product>
	  <product>
			<code>abc</code>
			<qty>1</qty>
	  </product>
	 </products>
	</data>
 </imported_data>
</import>

Open in new window


I have it sort of working using this but the data file is not inside 'imported_data'  it gets placed after it and before </import>

XmlDocument xmlTemplate = new XmlDocument();
                XmlDocument xmlData = new XmlDocument();

                xmlTemplate.Load(sHeaderFile);
                xmlData.Load(sDataFile);


                XmlNode node = xmlTemplate.ImportNode(xmlData.DocumentElement, true);
                xmlTemplate.DocumentElement.AppendChild(node);

                xmlTemplate.Save("C:\\temp\\output.xml");

Open in new window

IanPaskinAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Hi Ian;

You can achieve your desired results using the following code snippet.

using System.Xml.Linq;

// Load the template.xml document into memory
XDocument templateDoc = XDocument.Load("File-Path/template.xml");
// Load the data.xml document into memory
XDocument dataDoc = XDocument.Load("File-Path/data.xml");

// Get a reference to the imported_data node in the template
XElement impData = templateDoc.Root.Element("imported_data");
// Add the data.xml to the imported_data node as children
impData.Add(dataDoc.Root);

// Save the newly created XML document to the file system
templateDoc.Save("File-Path/desired_output.xml");

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
IanPaskinAuthor Commented:
Hi Fernando

Perfect... A well commented and explained solution, I did have a quick look at XML Linq before posting the question but not used it before, now seeing your solution it looks a lot more powerful than xmldocument, and what you suggested is almost what I was trying to but not with the right tools..

Thanks...
0
Fernando SotoRetiredCommented:
Not a problem Ian, glad I was able to help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.