我正在尝试将对象列表设为
List<FeedItem> List_feeds = new List<FeedItem>();
FeedItem objFeedItem = new FeedItem();
objFeedItem.feedId = feedNode.SelectSingleNode("id").InnerText;
.
.
.
.
objFeedItem.text = feedNode.SelectSingleNode("text").InnerXml;
objFeedItem.imgthumb = thumbImage;
objFeedItem.imglarge = largeImage;
List_feeds.Add(objFeedItem);
错误:“对象引用未设置为对象的实例。”
有人要求补充代码听到它是..
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using System.Net;
namespace FSB.Classes
{
public class FeedItem
{
public string feedId{ get; set; }
public string feed_publishDate{ get; set; }
public string feed_createDate{ get; set; }
public string feed_modifieddate{ get; set; }
public string title{ get; set; }
public string description{ get; set; }
public string text{ get; set; }
public string imgthumb{ get; set; }
public string imglarge{ get; set; }
public string addedBy{ get; set; }
public string edited{ get; set; }
public string createDate{ get; set; }
public string updateDat { get; set; }
}
public class Feeds
{
protected string newsURL;
protected List<FeedItem> List_feeds;
public Feeds()
{
newsURL = @"http://api.brafton.com/55f3e621-2223-48de-bafc-4f3774e72533/news/";
List<FeedItem> List_feeds = new List<FeedItem>();
}
public void updateAllFeeds()
{
getFeedList();
}
private void getFeedList()
{
try
{
XmlDocument objFeedDoc = new XmlDocument();
var nodeFeedList = getFeedNodes();
foreach (XmlNode objFeedNode in nodeFeedList)
{
string feedDetailhref = objFeedNode.Attributes["href"].Value;
string strFeedDetail = getFeedDetailXml(feedDetailhref);
objFeedDoc.LoadXml(strFeedDetail);
XmlNode feedNode = objFeedDoc.SelectSingleNode("newsItem");
FeedItem objFeedItem = new FeedItem();
objFeedItem.feedId =feedNode.SelectSingleNode("id").InnerText;
objFeedItem.feedId = feedNode.SelectSingleNode("id").InnerText;
objFeedItem.feed_publishDate = feedNode.SelectSingleNode("publishDate").InnerText;
objFeedItem.feed_createDate = feedNode.SelectSingleNode("createdDate").InnerText;
objFeedItem.feed_modifieddate = feedNode.SelectSingleNode("lastModifiedDate").InnerText;
objFeedItem.title = feedNode.SelectSingleNode("headline").InnerText;
objFeedItem.description = feedNode.SelectSingleNode("htmlMetaDescription").InnerText;
objFeedItem.text = feedNode.SelectSingleNode("text").InnerXml;
string thumbImage = "";
string largeImage = "";
/*
try
{
string pictureURL = feedNode.SelectSingleNode("photos").Attributes["href"].Value;
XmlNode imageNode = getFeedImage(pictureURL);
foreach (XmlNode pic in imageNode)
{
if (pic.SelectSingleNode("type").InnerText.ToLower() == "large")
{
largeImage = pic.SelectSingleNode("url").InnerText;
}
if (pic.SelectSingleNode("type").InnerText.ToLower() == "thumb")
{
thumbImage = pic.SelectSingleNode("url").InnerText;
}
}
}
catch (Exception ex)
{
Console.Write(ex.Message);
}
*/
objFeedItem.imgthumb = thumbImage;
objFeedItem.imglarge = largeImage;
List_feeds.Add(objFeedItem);
}
}
catch (Exception ex)
{
Console.Write(ex.Message);
}
}
private XmlNode getFeedImage(string pictureURL)
{
try
{
XmlDocument picDoc = new XmlDocument();
var client = new WebClient();
var content = client.DownloadString(newsURL);
picDoc.LoadXml(content);
return picDoc.SelectSingleNode("photos/photo/instances");
}
catch (Exception ex)
{
Console.Write(ex.Message);
}
return null;
}
/*Extract the newsListItem from the xml Feeds */
private XmlNodeList getFeedNodes()
{
try
{
XmlDocument docFeed = new XmlDocument();
string strFeedXml = getFeedXml();
docFeed.LoadXml(strFeedXml);
return docFeed.SelectNodes("news/newsListItem");
}
catch (Exception ex)
{
Console.Write(ex.Message);
}
return null;
}
/*Get the Xml Feeds From the Live Server*/
private string getFeedXml()
{
try
{
var client = new WebClient();
var content = client.DownloadString(newsURL);
return content;
}
catch (Exception ex)
{
Console.Write(ex.Message);
}
return null;
}
/*Get the Xml Feeds Details From the Live Server*/
private string getFeedDetailXml(string newsDetailURL)
{
try
{
var client = new WebClient();
var content = client.DownloadString(newsDetailURL);
return content;
}
catch (Exception ex)
{
Console.Write(ex.Message);
}
return null;
}
}
}
答案 0 :(得分:1)
您的声明
protected List<FeedItem> List_feeds;
永远不会被实例化。
将其更改为
protected List<FeedItem> List_feeds = new List<FeedItem>();
并从构造函数中删除它:
List<FeedItem> List_feeds = new List<FeedItem>();
这是因为构造函数中的代码只是创建一个与类级别变量同名的局部变量,而不是实际实例化一个类级别对象(我认为你想要的)
答案 1 :(得分:0)
var idNode = feedNode.SelectSingleNode("id");
if (idNode != null) {
objFeedItem.feedId = idNode.InnerText;
...
}
var textNode = feedNode.SelectSingleNode("text");
if (textNode != null) {
objFeedItem.text = textNode.InnerXml;
...
}
答案 2 :(得分:0)
List<FeedItem> List_feeds = new List<FeedItem>();
在构造函数中,您实例化的是局部变量而不是全局变量。