2012-10-15 18 views
7

tôi đang cố gắng tạo ra một đối tượng xmldocument bởi một XML khác nhauTạo XmlDocument từ tài liệu khác

xem mã dưới đây:

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 

Lỗi: Nút được chèn là từ một bối cảnh tài liệu khác nhau

Chỉnh sửa 1 Compleate Chặn mã:

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

Trả lời

10

Bạn cần phải sử dụng phương pháp Import Node để import XmlNode từ tài liệu đầu tiên vào bối cảnh thứ hai:

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

Bạn đang rất gần với câu trả lời của bạn, vấn đề chính bạn có bây giờ là bạn cần phải thêm phần tử tin tức của bạn vào tài liệu chính của bạn. Tôi muốn giới thiệu cách làm như sau nếu bạn muốn tài liệu đầu ra của bạn trông như thế này:

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

Thay vì tạo ra một XmlElement mới, tin tức, thay vào đó, khi bạn tạo docRSS, làm như sau:

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

Bây giờ bạn có một XmlDocument trông như thế này:

<news/> 

Sau đó, chứ không phải là news.AppendChild, chỉ cần:

docRSS.DocumentElement.AppendChild(importNewsItem); 

Điều này gắn thêm mỗi newsItem trong phần tử news (trong trường hợp này là phần tử tài liệu).

+0

+1 hoạt động. nhưng tôi vẫn nhận được docRss là trống rỗng khi tôi sử dụng 'docRss.Save (Response.Output);' để xuất tài liệu compleate – Champ

+0

Tôi đã thêm khối compleate của mã, bạn có thể vui lòng giúp đỡ? – Champ

+0

Bạn đang rất gần! Vấn đề là bạn đã tạo tất cả xml mà bạn muốn, nhưng sau đó bạn chưa thêm phần tử tin tức vào tài liệu đầu ra của mình. Xem câu trả lời cập nhật của tôi. – dash