Search code examples
c#xmlxmldocument

Create xmlDocument from another document


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);
 }

Solution

  • 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).