You need to use the Import Node method to import the XmlNode from the first document into the context of the second:
objNewsDoc.LoadXml(strNewsDetail); // Current XML XmlDocument docRss = new XmlDocument(); // new Xml Object i Want to create XmlElement news = docRss.CreateElement("news"); // creating the wrapper news node //Import the node into the context of the new document. NB the second argument = true imports all children of the node, too XmlNode importNewsItem = docRss.ImportNode(objNewsDoc.SelectSingleNode("newsItem"), true); news.AppendChild(importNewsItem);
EDIT
You are very close to your answer, the main problem that you are facing right now is that you need to add your news item to your main document. I would recommend doing the following if you want your output to look like this:
<news> <newsItem>...</newsItem> <newsItem>...</newsItem> </news>
Instead of creating a new XmlElement, news, instead, when creating docRSS, follow these steps:
XmlDocument docRss = new XmlDocument(); docRss.LoadXml("<news/>");
Now you have an XmlDocument that looks like this:
<news/>
Then, instead of news.AppendChild simply:
docRSS.DocumentElement.AppendChild(importNewsItem);
This adds each newsItem under the news element (which in this case is a document element).
dash
source share