What would be the fastest way, to merge 2 XML files, so I would locate a node in first one, empty it, take all children from the same tag (same node) in second XML and put it in the first one.
A:
Check out this article on MSDN: Article
I think this is the bit of code you are looking for:
try
{
XmlTextReader xmlreader1 = new XmlTextReader("C:\\Books1.xml");
XmlTextReader xmlreader2 = new XmlTextReader("C:\\Books2.xml");
DataSet ds = new DataSet();
ds.ReadXml(xmlreader1);
DataSet ds2 = new DataSet();
ds2.ReadXml(xmlreader2);
ds.Merge(ds2);
ds.WriteXml("C:\\Books.xml");
Console.WriteLine("Completed merging XML documents");
}
catch (System.Exception ex)
{
Console.Write(ex.Message);
}
Console.Read();
Hope that helps!
Zachary Yates
2008-12-05 00:50:41
That technique will only work for that very small subset of XML documents that can be parsed into DataSets, For instance, add a second <author> element under any of the <book> elements in the input, and you'll get a DuplicateNameException.
Robert Rossney
2008-12-05 01:31:54
He did ask for the quickest way.
Zachary Yates
2008-12-05 02:17:32
+4
A:
You can load both files into two XElement
objects, locate the target nodes in both objects and do the switch.
Here is a sample:
var nodes1 = XDocument.Parse(file1).Element("test").Element("nodes");
var nodes2 = XDocument.Parse(file2).Element("test").Element("nodes");
nodes1.Nodes().Remove();
nodes1.Add(nodes2.Nodes());
Here is the XML snippet i tried it on:
<test> <nodes> <node id="1"> Hi </node> <node id="2"> Hi again </node> <node id="3"> Hi once more </node> </nodes> </test>
Y Low
2008-12-05 03:20:59