I have a class that has this code,
pCollection pub = RSSXmlDeserializer.GetPub(path, fReload);
Get pub is the method that returns a collections of pub’s…
How can I iterate them. I tried,
for (var n = 0; n < pub.Count; n++) {
}
this is the getPub method
public static PCollection GetPub(string path, bool fReload)
{
HttpApplicationState session = HttpContext.Current.Application;
PCollection pub = session["PUB"] as PCollection;
if pub == null || fReload)
{
StreamReader reader = null;
try
{
XmlSerializer serializer = new XmlSerializer(typeof(PCollection));
reader = new StreamReader(path);
pub = (PCollection)serializer.Deserialize(reader);
session["PUB"] = pub;
}
catch (Exception ex)
{
//throw ex;
}
finally
{
reader.Close();
}
}
return pub;
}
}
[Serializable()]
public class Pub
{
[System.Xml.Serialization.XmlElement("title")]
public string Title { get; set; }
[System.Xml.Serialization.XmlElement("description")]
public string Description { get; set; }
[System.Xml.Serialization.XmlElement("imageUrl")]
public string ImageUrl { get; set; }
}
[Serializable()]
[System.Xml.Serialization.XmlRoot("RPublications")]
public class PCollection
{
[XmlArray("Pub")]
[XmlArrayItem("Pub", typeof(Pub))]
public Pub[] Pub { get; set; }
}
but ‘Count’ is not recognised. I get this message, pCollection does not have a definition for ‘Count’…
How do I iterate the collection n get the collection elements?
PCollectionis not really a collection. It is a class that contains a collection (more precisely an array). So to iterate you need to iterate the array:Or if you don’t care about the index and just want to go through the collection from start to finish:
(A more consistent naming of types and members would probably help.)