【发布时间】:2018-07-12 13:52:52
【问题描述】:
在下面的代码中,我找到了“Undly”元素,然后为类设置了适当的属性。这是正确的方法吗?还是我应该创建一个新的 XElement 类然后查找属性?
我仍在学习 XMLTextReader,并希望使用最佳实践。
另外,我不确定我应该使用接受 XML 文件的字符串(文件路径)的构造函数还是使用使用文件流的构造函数。似乎使用文件流的性能更快。
这里是代码
using (var fs = new FileStream(RBMRBHTheoretical, FileMode.Open, FileAccess.Read))
{
using (var xmlReader = new XmlTextReader(fs))
{
while (xmlReader.Read())
{
if (xmlReader.NodeType == XmlNodeType.Element)
{
if (string.Equals(xmlReader.Name, "Undly"))
{
occPLList.Add(new OccPL
{
Symbol = (string)xmlReader.GetAttribute("Sym").ToString().Trim(),
Description = (string)xmlReader.GetAttribute("Desc").ToString().Trim(),
Price = xmlReader.GetAttribute("Px") == null ? 0 : Convert.ToDecimal(xmlReader.GetAttribute("Px").ToString().Trim()),
Currency = (string)xmlReader.GetAttribute("Ccy").ToString().Trim()
});
}
}
}
}
}
public class OccPL
{
public string Description { get; set; }
public decimal Price { get; set; }
public string Currency { get; set; }
public string Symbol { get; set; }
}
这里是xml文件:
<FIXML r="20030618" s="20040109" v="4.4" xr="FIA" xv="1" xmlns="http://www.fixprotocol.org/FIXML-4-4" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://www.fixprotocol.org/FIXML-4-4 https://optionsclearing.com/components/docs/membership/dds_ref/fia_1_1/fixml-main-4-4-FIA-1-1.xsd">
<Batch>
<SecList ListTyp="109" ListID="20175" BizDt="2017-12-07">
<SecL Ccy="USD">
<Instrmt Desc="iShares S&P 100 ETF" SecTyp="OPT" SubTyp="ETO" Sym="OEF" Mult="100.0">
<AID AltID="00013" AltIDSrc="RBHP"/>
</Instrmt>
<InstrmtExt>
<Attrb Typ="101" Val="1.0000"/>
<Attrb Typ="108" Val="1.0000"/>
</InstrmtExt>
<Undly Desc="iShares S&P 100 ETF" Px="117.110000" Ccy="USD" Sym="OEF" ID="464287101" Src="1"/>
<Stip Typ="RBHMIN" Val="2.500"/>
<Stip Typ="CPMMIN" Val="3.750"/>
</SecL>
</SecList>
</Batch>
</FIXML>
【问题讨论】:
-
"这是正确的做法吗?" 有效吗? “似乎使用文件流的性能更快”如果没有实际指标,就无法帮助感知性能差异。
-
是的,它有效。我会尝试获取指标。
标签: c# filestream xmltextreader