0

试图从我的电脑上的 XML 文件中读取。该函数使用字符串值来传递静态函数。我想为每笔交易创建一个我猜的所谓的“节点”。我想说数组数组,因为我来自 C++/Java 背景。每个 List 中都有一个内容列表。交易列表,在每个交易中,我都有与该交易对应的股票价值、行动价值、日期价值和股票价值。这就是我到目前为止所拥有的。

XML 文件

<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# 代码

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)

        }
4

1 回答 1

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;
    }
于 2013-02-18T03:38:13.710 回答