Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8707541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:53:38+00:00 2026-06-13T03:53:38+00:00

I am trying create a xmldocument object by a different XML see the code

  • 0

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);
 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T03:53:42+00:00Added an answer on June 13, 2026 at 3:53 am

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create an xml with the following code. XmlDocument xmlDocument =
I'm trying to dynamically create an object of a certain type in a LINQ-to-XML
I am trying to create an XSLT to transform an XML document and having
Trying to create a new Dedicated Cache Role in Windows Azure but get the
I'm trying to recreate a SyndicationFeed object (System.ServiceModel.Syndication) from XML data that has been
I'm new to Linq and I'm trying to query a XML document to find
I am trying to Serialize a Class object and store the xml in a
I would like to create a new System.Windows.Xps.Packaging.XpsDocument object from byte array, as I
The following code snippet returns xml document public XmlDocument GetXMLFile(int ID) { List<UserInfoBE> data
Using WP7 & C#: I'm trying to convert my object(s) to XML so I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.