我是解析 XML 的新手,我开始学习 linq,我认为它可能是这里最好的解决方案。我最感兴趣的是性能,因为我正在创建的应用程序将读取证券交易所的价格,有时价格变化非常快。 我从服务器收到以下消息:
<?xml version="1.0" encoding="utf-16"?>
<events>
<header>
<seq>0</seq>
</header>
<body>
<orderBookStatus>
<id>100093</id>
<status>Opened</status>
</orderBookStatus>
<orderBook>
<instrumentId>100093</instrumentId>
<bids>
<pricePoint>
<price>1357.1</price>
<quantity>20</quantity>
</pricePoint>
<pricePoint>
<price>1357.0</price>
<quantity>20</quantity>
</pricePoint>
<pricePoint>
<price>1356.9</price>
<quantity>71</quantity>
</pricePoint>
<pricePoint>
<price>1356.8</price>
<quantity>20</quantity>
</pricePoint>
</bids>
<offers>
<pricePoint>
<price>1357.7</price>
<quantity>51</quantity>
</pricePoint>
<pricePoint>
<price>1357.9</price>
<quantity>20</quantity>
</pricePoint>
<pricePoint>
<price>1358.0</price>
<quantity>20</quantity>
</pricePoint>
<pricePoint>
<price>1358.1</price>
<quantity>20</quantity>
</pricePoint>
<pricePoint>
<price>1358.2</price>
<quantity>20</quantity>
</pricePoint>
</offers>
<lastMarketClosePrice>
<price>1356.8</price>
<timestamp>2011-05-03T20:00:00</timestamp>
</lastMarketClosePrice>
<dailyHighestTradedPrice />
<dailyLowestTradedPrice />
<valuationBidPrice>1357.1</valuationBidPrice>
<valuationAskPrice>1357.7</valuationAskPrice>
<lastTradedPrice>1328.1</lastTradedPrice>
<exchangeTimestamp>1304501070802</exchangeTimestamp>
</orderBook>
</body>
</events>
我的目标是解析价格点元素
<pricePoint>
<price>1358.2</price>
<quantity>20</quantity>
</pricePoint>
进入以下结构的字典:
Dictionary<double, PriceLevel>
其中 price 应该是 double 而 PriceLevel 是一个类
class PriceLevel
{
int bid;
int offer;
public PriceLevel(int b, int o)
{
bid = b;
offer = o;
}
}
根据存在每个价格点(出价或报价)的元素,应相应地分配数量,即,如果价格点存在于出价中,则应将数量分配给出价,将 0 分配给报价。相反,如果报价中存在价格点,则应将数量分配给报价,将 0 分配给报价。
我希望我的解释很清楚,但是如果您在理解上有任何问题,请随时在评论中要求澄清。 如果能帮助解决这个问题,我将不胜感激。
++++++++++++++++++++++++++++++++++++++++++ 更新:
我已经深入了解了我正在尝试阅读的流,它不会像我预期的那么简单。我发现,流并不总是包含整个文档,因此我将不得不使用 XmlReader 读取它以持续处理流。在这种情况下,我如何读取出价和报价?我有这样的东西:
StreamReader sr = new StreamReader("..\..\videos.xml");
XmlReader xmlReader = XmlReader.Create(sr);
while (xmlReader.Read())
{
if (xmlReader.HasValue)
{
OnXmlValue(this, new MessageEventArgs(true, xmlReader.Value));//saxContentHandler.Content(xmlReader.Value);
}
else
{
if (xmlReader.IsEmptyElement)
{
OnStartElement(this, new MessageEventArgs(false, xmlReader.Name));
OnEndElement(this, new MessageEventArgs(false, xmlReader.Name));
}
else if (xmlReader.IsStartElement())
{
OnStartElement(this, new MessageEventArgs(false, xmlReader.Name));
}
else
{
OnEndElement(this, new MessageEventArgs(false, xmlReader.Name));
}
}
}
但我正在努力将元素名称与其值联系起来……也就是说,我如何才能知道我当前正在阅读的是哪个出价点,以及该点是否存在于出价或报价中? 谢谢你的帮助
最佳答案
当您使用基于事件的界面时,类似于您更新中出现的界面,您将需要记住前一个开始元素事件的名称。通常值得持有一个堆栈来跟踪事件。我可能会做类似以下的事情:
public class PriceLevel
{
private decimal? bid = null;
private decimal? offer = null;
public decimal? Bid {
get { return bid; }
set { bid = value; }
}
public decimal? Offer {
get { return offer; }
set { offer = value; }
}
}
public delegate void OnPriceChange(long instrumentId, Dictionary<decimal, PriceLevel> prices);
public class MainClass
{
private Stack<String> xmlStack = new Stack<String>();
private Dictionary<decimal, PriceLevel> prices = new Dictionary<decimal, PriceLevel>();
private bool isBids = false;
private decimal? currentPrice = null;
private long instrumentId;
private OnPriceChange _priceChangeCallback;
public void MainClass(OnPriceChange priceChangeCallback) {
this._priceChangeCallback = priceChangeCallback;
}
public void XmlStart(object source, MessageEventArgs args) {
xmlStack.Push(args.Value);
if (!isBids && "bids" == args.Value) {
isBids = true;
}
}
public void XmlEnd(object source, MessageEventArgs args) {
xmlStack.Pop();
if (isBids && "bids" == args.Value) {
isBids = false;
}
// Finished parsing the orderBookEvent
if ("orderBook" == args.Value) {
_priceChangeCallback(instrumentId, prices);
}
}
public void XmlContent(object source, MessageEventArgs args) {
switch (xmlStack.Peek()) {
case "instrumentId":
instrumentId = long.Parse(args.Value);
break;
case "price":
currentPrice = decimal.Parse(args.Value);
break;
case "quantity":
if (currentPrice != null) {
decimal quantity = decimal.Parse(args.Value);
if (prices.ContainsKey(currentPrice)) {
prices[currentPrice] = new PriceLevel();
}
PriceLevel priceLevel = prices[currentPrice];
if (isBids) {
priceLevel.Bid = quantity;
} else {
priceLevel.Offer = quantity;
}
}
break;
}
}
}
关于c# - 将此 XML 读取到字典的最快/最有效的方法是什么(Linq 或其他?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5881599/
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时
我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
好的,所以我的目标是轻松地将一些数据保存到磁盘以备后用。您如何简单地写入然后读取一个对象?所以如果我有一个简单的类classCattr_accessor:a,:bdefinitialize(a,b)@a,@b=a,bendend所以如果我从中非常快地制作一个objobj=C.new("foo","bar")#justgaveitsomerandomvalues然后我可以把它变成一个kindaidstring=obj.to_s#whichreturns""我终于可以将此字符串打印到文件或其他内容中。我的问题是,我该如何再次将这个id变回一个对象?我知道我可以自己挑选信息并制作一个接受该信
它不等于主线程的binding,这个toplevel作用域是什么?此作用域与主线程中的binding有何不同?>ruby-e'putsTOPLEVEL_BINDING===binding'false 最佳答案 事实是,TOPLEVEL_BINDING始终引用Binding的预定义全局实例,而Kernel#binding创建的新实例>Binding每次封装当前执行上下文。在顶层,它们都包含相同的绑定(bind),但它们不是同一个对象,您无法使用==或===测试它们的绑定(bind)相等性。putsTOPLEVEL_BINDINGput