0

Trying to read from an XML file that I have on my pc. The function uses a string value to pass the static function. I want to each transaction, create a so called "node" I guess. I want to say Array of Arrays since I come from C++/Java background. Every List has a list of things in it. List of Transactions, and in everyone transaction I have the ticker value, action value, date value, and shares value corresponding with that transaction. This is what I have so far.

XML File

<stocks>
<transaction>
<ticker>GOOG</ticker>
<action>buy</action>
<date>20071116</date>
<shares>44</shares>
</transaction>
<transaction>
<ticker>IBMX</ticker>
<action>buy</action>
<date>20080104</date>
<shares>350</shares>
</transaction>

C# Code

public static  void readXML(string filename)
    {
        XmlTextReader reader = new XmlTextReader(filename);
        List<List<string>> transList = new List<List<string>>();
        XmlNode node = 

        foreach(var transaction in reader())
        {
            transList.Add(new Transaction(node.get ticker,node.action,node.date,node.shares)

        }

1 Answers1

0
    public class Transaction
    {
        public string Ticker {get; set;}
        public string Action {get; set;}
        public string Date {get; set;}
        public int NumShares {get; set;}
    }

    private List<Transaction> getTransactionsFromFile(string filename)
    {
        XmlDocument xDoc = new XmlDocument();
        xDoc.Load(filename);
        List<Transaction> tranList = new List<Transaction>();

        foreach (XmlNode x in xDoc)
        {
            if (x.Name == "stocks")
            {
                foreach (XmlNode y in x.ChildNodes)
                {
                    if (y.Name == "transaction")
                    {
                        Transaction tempTran = new Transaction();
                        foreach (XmlNode z in y.ChildNodes)
                        {
                            if (z.Name == "ticker")
                            {
                                tempTran.Ticker= z.InnerText;
                            }
                            if (z.Name == "action")
                            {
                                tempTran.Action= z.InnerText;
                            }
                            if (z.Name == "date")
                            {
                                tempTran.Date = z.InnerText;
                            }
                            if (z.Name == "shares")
                            {
                                tempTran.NumShares = int.Parse(z.InnerText);
                            }

                            if (z.Name == "PassOutputToChildSteps")
                        }
                        //add the constructed Transaction Object to the Transaction List...
                        tranList.Add(tempTran);
                    }
                }
            }
        }
        return tranList;
    }
John Bartels
  • 2,583
  • 3
  • 19
  • 26
  • Any reason you used XMLDocument instead of XDocument? – Gero Feb 18 '13 at 10:56
  • I was not actually aware of XDocument, but I might try using it in the future. I just used XMLDocument because I had the example code readily available. – John Bartels Feb 18 '13 at 23:44