Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
 }
like image 817
GajendraSinghParihar Avatar asked Oct 15 '12 11:10

GajendraSinghParihar


People also ask

Should I use XDocument or XmlDocument?

XDocument is from the LINQ to XML API, and XmlDocument is the standard DOM-style API for XML. If you know DOM well, and don't want to learn LINQ to XML, go with XmlDocument . If you're new to both, check out this page that compares the two, and pick which one you like the looks of better.

What is the difference between XmlNode and XmlElement?

Every XmlElement is XmlNode, but not every XmlNode is XmlElement. XmlElement is just one kind of XmlNode. Others are XmlAttribute, XmlText etc. An Element is part of the formal definition of a well-formed XML document, whereas a node is defined as part of the Document Object Model for processing XML documents.

Is XmlDocument disposable?

XmlDocument can't be disposed because it does not implement IDisposable.


1 Answers

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

like image 196
dash Avatar answered Sep 21 '22 10:09

dash