0

のにXMLNode item追加する必要がXMLNode targetNodeありますXMLDocument docRss

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

例外: オブジェクト参照がオブジェクトのインスタンスに設定されていません。

編集targetNode が nullであることがわかりましたが、なぜですか? 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

これ targetNodeはnullなので、間違って選択していました。

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