0

我有一个XMLNode item要附加到XMLNode targetNodeXMLDocument docRss

 XmlNode targetNode = docRss.SelectSingleNode("channel");
 targetNode .AppendChild(docRss.ImportNode(item, true));

异常:对象引用未设置为对象的实例。

编辑我发现targetNode 为空,但为什么呢?如在 docRss.innerXml 中向我们展示它的存在

在此声明之前的值是:

docRss.innerXml   = <?xml version=\"1.0\"?><rss version=\"2.0\"><channel><title>The federal Savings Bank News Feeds</title><link>https://www.thefederalsavingsbank.com</link><description>The federal Savings Bank News Feeds</description><language>en-us</language></channel></rss>

item.innerXml  = <title>Housing market improvement helps economy</title><link>https://www.thefederalsavingsbank.com/Advice</link><description>&amp;lt;p&amp;gt;According to The Associated Press, a strong housing report helped improve the stock market, showing that the housing market affects more than just one industry.&amp;lt;/p&amp;gt;\r\n&amp;lt;p&amp;gt;The Dow increased by 5.22 points to close at 13,557, which marked the fourth straight day of gains, which is thought to be a result of the improving housing market.&amp;lt;/p&amp;gt;\r\n&amp;lt;p&amp;gt;Time Magazine recently reported that a recovery for the housing market is also great news for large banks. The Department of Commerce announced that new residential construction projects increased by 15 percent in September in comparison to the prior month, and they also increased by 34.8 percent compared to last year.&amp;lt;/p&amp;gt;\r\n&amp;lt;p&amp;gt;Housing starts are still down from the numbers that were seen before the recession, but the market is steadily improving every month.&amp;lt;/p&amp;gt;\r\n&amp;lt;p&amp;gt;First-time home buyers who are interested in purchasing a home might look at now as the perfect time considering the low cost of mortgages and affordable rates that are offered. Those considering buying their first home should look into FHA loans considering they offer lower interest rates.&amp;lt;/p&amp;gt;</description>
4

2 回答 2

2

看起来您正试图操纵 RSS 提要。使用System.ServiceModel.Syndication命名空间的成员不会消除很多痛点吗?例如 SyndicationFeed 和 Rss20FeedFormatter...

要将节点添加到提要中,请尝试以下操作...

void RetargetFeed()
{
    string feedLocation = "http://example.com/rssfeed";

    // read original feed
    Rss20FeedFormatter rssformat = new Rss20FeedFormatter();
    rssformat.ReadFrom(XmlReader.Create(feedLocation));

    // create a list of items from the rogiinal
    List<SyndicationItem> items = new List<SyndicationItem>();
    items.AddRange(rssformat.Feed.Items);

    // add a new item to the end of the list
    items.Add(new SyndicationItem("Test Item", "This is the content for Test Item", new Uri("http://Contoso/ItemOne"), "TestItemID", DateTime.Now));

    // create a new Rss writer
    SyndicationFeed newFeed = new SyndicationFeed(items);
    var writeFormat = new Rss20FeedFormatter(newFeed);
    //and write the output to a file
    writeFormat.WriteTo(XmlWriter.Create("testoutputfile.xml"));
}
于 2012-10-19T12:32:19.760 回答
1

is null 所以我选错了 targetNode它必须选为

 XmlNode channel = docRss.SelectSingleNode("rss/channel");
于 2012-10-19T12:49:30.087 回答