Here is an example of copying data "xml" from one document to another.With selection of personalized node
First you need convert Xdocument to XmlDocument:
using System;
using System.Xml;
using System.Xml.Linq;
namespace MyTest
{
internal class Program
{
private static void Main(string[] args)
{
var xmlDocument = new XmlDocument();
xmlDocument.LoadXml("<Root><Child>Test</Child></Root>");
var xDocument = xmlDocument.ToXDocument();
var newXmlDocument = xDocument.ToXmlDocument();
Console.ReadLine();
}
}
public static class DocumentExtensions
{
public static XmlDocument ToXmlDocument(this XDocument xDocument)
{
var xmlDocument = new XmlDocument();
using(var xmlReader = xDocument.CreateReader())
{
xmlDocument.Load(xmlReader);
}
return xmlDocument;
}
public static XDocument ToXDocument(this XmlDocument xmlDocument)
{
using (var nodeReader = new XmlNodeReader(xmlDocument))
{
nodeReader.MoveToContent();
return XDocument.Load(nodeReader);
}
}
}
}
And now simplified copy with XmlDocument
XmlDocument doc1 = new XmlDocument();
doc1.LoadXml(@"<Hello>
<World>Test</World>
</Hello>");
XmlDocument doc2 = new XmlDocument();
doc2.LoadXml(@"<Hello>
</Hello>");
XmlNode copiedNode = doc2.ImportNode(doc1.SelectSingleNode("/Hello/World"), true);
doc2.DocumentElement.AppendChild(copiedNode);
more information here:
- http://blog.project-sierra.de/archives/1050
- Copy Xml element to another document in C#
- Converting XDocument to XmlDocument and vice versa
I hope this help you.