我在网站上托管了一个XML文件,我需要使用XMLText Reader阅读。在我阅读时,我需要在XML文档中添加项目,因为我正在将它们读取到类列表中。我不确定我应该通过什么参数和foreach部分。
transList是我的List,Transaction是我的类。它们已被全局定义在顶部,以供将来使用XML Serializer写入我已编写的XML文件。
包含多个交易的XML文件
<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))
}
答案 0 :(得分:1)
试试这个:
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; }
}
void ReadPortfolio(string filename)
{
if (File.Exists(filename))
{
var transList = new List<Transaction>();
foreach (XElement transaction in XDocument.Load(filename).Descendants("transaction"))
{
XElement ticker = transaction.Element("ticker");
XElement action = transaction.Element("action");
XElement date = transaction.Element("date");
XElement shares = transaction.Element("shares");
XElement price = transaction.Element("price");
transList.Add(new Transaction
{
Ticker = ticker != null ? ticker.Value : string.Empty,
Action = action != null ? action.Value : string.Empty,
Date = date != null ? date.Value : string.Empty,
NumShares = shares != null ? int.Parse(shares.Value) : default(int),
Price = price != null ? double.Parse(price.Value) : default(double)
});
}
}
}
此方法处理可能缺少元素的情况,并且我稍微重写了您的代码以更符合约定。它还检查文件是否存在。
这看起来像是一个财务应用程序,所以我还应该指出,您最好使用decimal
而不是double
。由于decimal
的浮点精度问题,double
被视为处理资金的合适方式。
答案 1 :(得分:0)
以下是如何使用XDocument进行操作...只需使用文件名取消注释行(并注释解析常量的行!)并传入文件名。
class Program
{
static void Main(string[] args)
{
var test = new AnotherXmlTest();
test.readPortfolio("filename");
//Put a break on this line and instect object 'test'
Console.ReadLine();
}
}
public class AnotherXmlTest
{
public const string xml = @"<portfolio><transaction><ticker>GOOG</ticker><action>buy</action><date>20071116</date><shares>44</shares></transaction></portfolio>";
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>();
public void readPortfolio(string filename)
{
//Use this instead!
//XmlTextReader reader = new XmlTextReader(filename);
//XDocument xDoc = XDocument.Load(reader);
XDocument xDoc = XDocument.Parse(xml);
transList.AddRange(from transaction in xDoc.Descendants("transaction")
select new Transaction
{
ticker = transaction.Element("ticker").Value,
action = transaction.Element("action").Value,
date = transaction.Element("date").Value,
numShares = Convert.ToInt32(transaction.Element("shares").Value)
// No price??
});
}
}
答案 2 :(得分:0)
如果你想获得漂亮,other one I provided的替代方法是使用LINQ:
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; }
}
void ReadPortfolio(string filename)
{
if (File.Exists(filename))
{
var transList = new List<Transaction>();
transList.AddRange(from transaction in XDocument.Load(filename).Descendants("transaction")
let ticker = transaction.Element("ticker")
let action = transaction.Element("action")
let date = transaction.Element("date")
let shares = transaction.Element("shares")
let price = transaction.Element("price")
select new Transaction
{
Ticker = ticker != null ? ticker.Value : string.Empty,
Action = action != null ? action.Value : string.Empty,
Date = date != null ? date.Value : string.Empty,
NumShares = shares != null ? int.Parse(shares.Value) : default(int),
Price = price != null ? double.Parse(price.Value) : default(double)
});
}
}