I have an XML file hosted on a website that I need to read using XMLText Reader. As I am reading, I need to add the items from the XML document as I am reading them to the list of classes. I'm not sure what parameters I should be passing through and the foreach section.
transList is my List and Transaction is my class. They have been globally defined at the top for future use for XML Serializer to write to an XML file that I have already written.
XML File with multiple transactions
<portfolio>
<transaction>
<ticker>GOOG</ticker>
<action>buy</action>
<date>20071116</date>
<shares>44</shares>
</transaction>
public class Transaction
{
public string ticker { get; set; }
public string action { get; set; }
public string date { get; set; }
public int numShares { get; set; }
public double price { get; set; }
}
List<Transaction> transList = new List<Transaction>();
void readPortfolio(string filename)
{
XmlTextReader reader = new XmlTextReader(filename);
reader.WhitespaceHandling = WhitespaceHandling.None;
foreach(var transaction in reader) //for each reader node equal to “transaction” do:
{
TransList.add(Transaction tr = new Transaction(ticker, action, date, number of shares))
}