从XMLText阅读器读取并添加到类列表中
本文关键字:添加 列表 读取 XMLText | 更新日期: 2023-09-27 18:00:04
我在一个网站上托管了一个XML文件,需要使用XMLText Reader读取该文件。在阅读时,我需要将XML文档中的项添加到类列表中。我不确定我应该通过什么参数和foreach部分。
transList是我的List,Transaction是我的类。它们在顶部进行了全局定义,以供XML序列化程序将来写入我已经编写的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))
}
试试这个:
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
。由于double
的浮点精度问题,decimal
被认为是处理货币的合适方式。
以下是如何使用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??
});
}
}
如果你想获得漂亮,我提供的另一种方法的替代方法是使用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)
});
}
}