9

I am trying create a xmldocument object by a different XML

see the code below:

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
news.AppendChild(objNewsDoc.SelectSingleNode("newsItem")); // adding the news item from old doc

Error: The node to be inserted is from a different document context

Edit 1 Compleate Block of code:

try
{
       XmlDocument objNewsDoc = new XmlDocument();
        string strNewsXml = getNewsXml();
        objNewsDoc.LoadXml(strNewsXml);

        var nodeNewsList = objNewsDoc.SelectNodes("news/newsListItem");
        XmlElement news = docRss.CreateElement("news");
         foreach (XmlNode objNewsNode in nodeNewsList)
         {
               string newshref = objNewsNode.Attributes["href"].Value;
                string strNewsDetail = getNewsDetailXml(newshref);
                 try
                  {
                        objNewsDoc.LoadXml(strNewsDetail);
                         XmlNode importNewsItem = docRss.ImportNode(objNewsDoc.SelectSingleNode("newsItem"), true);
                        news.AppendChild(importNewsItem);
                   }
                    catch (Exception ex)
                    {
                            Console.Write(ex.Message);
                      }

              }

             docRss.Save(Response.Output);
}
catch (Exception ex)
{
      Console.Write(ex.Message);
 }
GajendraSinghParihar
  • 9,051
  • 11
  • 36
  • 64

1 Answers1

12

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 issue you have now is that you need to append your news element to your main document. I would recommend doing the following if you want your output document to look like this:

<news>
  <newsItem>...</newsItem>
  <newsItem>...</newsItem>
</news>

Rather than create a new XmlElement, news, instead, when you create docRSS, do the following:

XmlDocument docRss = new XmlDocument();
docRss.LoadXml("<news/>");

You now have an XmlDocument that looks like this:

<news/>

Then, rather than news.AppendChild, simply:

docRSS.DocumentElement.AppendChild(importNewsItem);

This appends each newsItem under the news element (which in this instance is the document element).

dash
  • 89,546
  • 4
  • 51
  • 71
  • +1 that worked. but i am still getting the docRss as empty when i use the ` docRss.Save(Response.Output);` to output the compleate doc – GajendraSinghParihar Oct 15 '12 at 12:07
  • You are so close! The problem is that you've created all of the xml you want, but you haven't then appended the news element to your output document. See my updated answer. – dash Oct 15 '12 at 12:25