我正在尝试通过不同的XML创建一个
请参考下面的代码:
xmldocument
对象。请参考下面的代码:
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);
}
docRss.Save(Response.Output);
输出完整文档时,仍然将docRss
作为空白。 - GajendraSinghParihar