MuraliKrishna
MuraliKrishna

Reputation: 51

How to Replace one Xml Node with another in C#

How replace XML Node from one XML document with another XML node from another XML Document. Please help..

Upvotes: 2

Views: 3961

Answers (1)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236188

You can use LINQ to Xml XElement.ReplaceWith method

// select node from one doc
XDocument xdoc1 = XDocument.Load(path_to_doc1);    
XElement one = xdoc1.Descendants("One").First(); 

// select node from another doc
XDocument xdoc2 = XDocument.Load(path_to_doc2);
XElement another = xdoc2.Descendants("Another").First(); 

// replace one xml node with another
one.ReplaceWith(another);
xdoc1.Save(path_to_doc1);

Upvotes: 4

Related Questions