[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 377
  • Last Modified:

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

0
IanPaskin
Asked:
IanPaskin
  • 2
1 Solution
 
Fernando SotoCommented:
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
 
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 SotoCommented:
Not a problem Ian, glad I was able to help.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now