xmldocument
別の XML でオブジェクトを作成しようとしています
以下のコードを参照してください。
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
エラー:挿入するノードは別のドキュメント コンテキストからのものです
コードの 1 つの完全なブロックを編集します。
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);
}