我是非常新的解析XML,我開始學習linq,我認爲這可能是最好的解決方案。我最感興趣的是性能,因爲我創建的應用程序將讀取股票交易所的價格,這些價格有時可能會非常迅速地變化。 我從服務器接收以下信息:什麼是最快/最有效的方式來閱讀這個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>
在價格應該是一個雙和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));
}
}
}
,但我很努力的元素名稱鏈接到它的價值...即,我怎麼能知道哪些標價點我目前正在讀,如果存在的話在投標或報價? 謝謝你的幫助
什麼是您的價格點的最大準確性?你有他們所有的小數點後1位,在dp之前有4位數字,在你的例子中。這是如何爲您的所有價格點? – 2011-05-04 10:09:30
速度最快有多快?秒,毫秒,微秒? – Ishtar 2011-05-04 10:28:10
我認爲,6位小數的最大精度應該足夠了。至於速度和性能,每秒鐘會有很多類似的消息,所以我至少要說幾毫秒;) – Macin 2011-05-04 11:24:21