我正在尝试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);
}