Reputation: 13
I've got the following:
// Begin the WebRequest to the desired RSS Feed
WebRequest myRequest = WebRequest.Create(url);
WebResponse myResponse = myRequest.GetResponse();
// Convert the RSS Feed into an XML document
Stream rssStream = myResponse.GetResponseStream();
XmlDocument rssDoc = new XmlDocument();
rssDoc.Load(rssStream);
// This uses an XPath expression to get all nodes that fall
// under this path.
XmlNodeList rssItems = rssDoc.GetElementsByTagName("row");
ArrayList returnArrayList = new ArrayList();
CharData cd = new CharData();
for (int i = 0; i < rssItems.Count; i++)
{
cd.CharacterName = rssItems[i].Attributes["name"].Value;
cd.CharacterID = rssItems[i].Attributes["characterID"].Value;
cd.CorporationID = rssItems[i].Attributes["corporationID"].Value;
cd.CorporationName = rssItems[i].Attributes["corporationName"].Value;
}
this.richTextBox1.Text = cd.CharacterName+"\r\n"+cd.CharacterID+"\r\n"+cd.CorporationID+"\r\n"+cd.CorporationName+"\r\n";
CharData Class:
class CharData
{
private string _charName;
private string _charID;
private string _corporationID;
private string _corpName;
public string CharacterName
{
get { return _charName; }
set { _charName = value; }
}
public string CharacterID
{
get { return _charID; }
set { _charID = value; }
}
public string CorporationID
{
get { return _corporationID; }
set { _corporationID = value; }
}
public string CorporationName
{
get { return _corpName; }
set { _corpName = value; }
}
}
Now, how do I add multiple chars data the system and retrieve them later?
Upvotes: 0
Views: 2544
Reputation: 974
It looks like your objects have an ID so a Dictionary might be better if you need to access by ID. Alternatively, if each CharData is displayed in it's own control item, such as a gridrow, you could link the CharData object to the control's .Tag property.
Upvotes: 0
Reputation: 16162
Declare new CharData
inside the for loop, and then add it to array. like:
List<CharData> charDataList = new List<CharData>();
for (int i = 0; i < rssItems.Count; i++)
{
CharData cd = new CharData();
cd.CharacterName = rssItems[i].Attributes["name"].Value;
cd.CharacterID = rssItems[i].Attributes["characterID"].Value;
cd.CorporationID = rssItems[i].Attributes["corporationID"].Value;
cd.CorporationName = rssItems[i].Attributes["corporationName"].Value;
charDataList.Add(cd);
}
Upvotes: 1
Reputation: 268215
Firstly, you shouldn't use ArrayList
when possible because it's not typesafe and is only kept for backward compatibility. Since .NET 2.0, generic List<T>
should be used instead.
I believe you're trying to do this:
var returnItems = new List<CharData>();
foreach (var rssItem in rssItems) {
returnItems.Add(new CharData {
CharacterName = rssItem.Attributes["name"].Value,
CharacterID = rssItem.Attributes["characterID"].Value,
CorporationID = rssItem.Attributes["corporationID"].Value,
CorporationName = rssItem.Attributes["corporationName"].Value
});
}
Also, LINQ to XML is the preferred API for extracting data from XML since .NET 3.5—and it's simpler, too. You can rewrite your sample as follows:
var doc = XDocument.Load(rssStream);
var returnItems = (from rssItem in doc.Descendants("row")
select new CharData {
CharacterName = rssItem.Attribute("name"),
CharacterID = rssItem.Attribute("characterID"),
CorporationID = rssItem.Attribute("corporationID"),
CorporationName = rssItem.Attribute("corporationName")
}).ToList();
Upvotes: 1
Reputation: 31559
You should use generics:
System.Collection.Generic.List<CharData> returnArrayList = new System.Collection.Generic.List<CharData>();
In any case(generic or the current non generic) you can add item using
returnArrayList.Add(cd);
Upvotes: 0