簡體   English   中英

閱讀此 XML 到字典(Linq 或其他東西?)的最快/最有效的方法是什么?

[英]What is the fastest/most efficient way to read this XML to Dictionary (Linq or something else?)

我對解析 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

class PriceLevel
{
     int bid;
     int offer;

     public PriceLevel(int b, int o)
     {
          bid = b;
          offer = o;
     }


}

根據每個價格點所在的元素(投標或報價),應相應地分配數量,即,如果投標中存在價格點,則應將數量分配給投標,將 0 分配給報價。 相反,如果報價中存在價格點,則應將數量分配給報價,將 0 分配給投標。

我希望我的解釋很清楚,但是如果您對理解有任何疑問,請隨時在評論中要求澄清。 我將不勝感激幫助解決這個問題。

+++++++++++++++++++++++++++++++++++++++++++++ 更新:

我已經深入研究了我正在嘗試閱讀的 stream,它不會像我預期的那么簡單。 我發現,stream 並不總是包含整個文檔,因此我必須使用 XmlReader 讀取它以持續處理 stream。 在這種情況下,我如何閱讀出價和出價? 我有這樣的事情:

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;
        }
    }
}

首先,您需要獲得所有報價和所有出價

XDocument xmlDoc = XDocument.Load("TestFile.xml");


var bids = (from b in xmlDoc.Descendants("bids")
           select b).ToList();

var offers = (from o in xmlDoc.Descendants("offers")
           select o).ToList();

然后您只需遍歷出價和出價並將它們添加到字典中...但是正如有人之前所說...您可能會遇到這樣的問題,即如果價格水平相同,則出價和出價都會設置

遍歷列表,您只需執行此操作

foreach (XElement e in bids)
{
   price = e.Element("price").Value;
   quantity = e.Element("quantity").Value;
   dictionary.add(price, new PriceLevel(quantity,null);
}

與您提供的相同...但是再次...您可能必須檢查此密鑰是否已經存在...

首先,我相信您輸入字典的方法會導致錯誤。 如果沒有錯,字典不能有相同的鍵,所以由於你使用價格作為鍵,你很有可能遇到這個問題。

我不能說速度,你必須測試一下。 但到目前為止,XDocument 對我來說運行良好。
例如,使用 XDocument,將整個 xml 消息加載到該變量中

XDocument doc = XDocument.Load(message);

使用 doc,您可以使用 Linq 將它們分組為出價和詢價。

一旦你實現了這一點,展示你的數據應該沒有問題,因為你已經得到了價格並將它們分成投標和要價

我設法得到這樣的東西:

public void messageParser()
    {
        int i = 0;
        bool readingBids = false;
        bool readingOffers = false;
        decimal price=0;
        int qty = 0;

        StreamReader sr = new StreamReader("..\\..\\sampleResponse.xml");

        XmlReader xmlReader = XmlReader.Create(sr);
        DateTime startTime = DateTime.Now;
        while (xmlReader.Read())
        {
            #region reading bids
            if (xmlReader.IsStartElement("bids"))
            {
                readingBids = true; 
                readingOffers = false; 
            }

            if (xmlReader.NodeType == XmlNodeType.EndElement && xmlReader.Name == "bids")
            {
                readingBids = false;
                readingOffers = false;
            }

            if (readingBids == true)
            {
                if (xmlReader.IsStartElement("price"))
                    price = xmlReader.ReadElementContentAsDecimal();

                if (xmlReader.IsStartElement("quantity"))
                {
                    qty = xmlReader.ReadElementContentAsInt();
                    OnPricePointReceived(this, new MessageEventArgs(price, qty, "bid"));
                }
            }
            #endregion

            #region reading offers
            if (xmlReader.IsStartElement("offers"))
            { 
                readingBids = false; 
                readingOffers = true; 
            }

            if (xmlReader.NodeType == XmlNodeType.EndElement && xmlReader.Name == "offers")
            {
                readingBids = false;
                readingOffers = false;
            }

            if (readingOffers == true)
            {
                if (xmlReader.IsStartElement("price"))
                    price = xmlReader.ReadElementContentAsDecimal();

                if (xmlReader.IsStartElement("quantity"))
                {
                    qty = xmlReader.ReadElementContentAsInt();
                    OnPricePointReceived(this, new MessageEventArgs(price, qty, "offer"));
                }
            }
            #endregion
        }
        DateTime stopTime = DateTime.Now;
        Console.WriteLine("time: {0}",stopTime - startTime);
        Console.ReadKey();
    }
}

這是解決問題的正確方法嗎? 我對這段代碼有一些疑問:

 if (readingBids == true)
        {
            if (xmlReader.IsStartElement("price"))
                price = xmlReader.ReadElementContentAsDecimal();

            if (xmlReader.IsStartElement("quantity"))
            {
                qty = xmlReader.ReadElementContentAsInt();
                OnPricePointReceived(this, new MessageEventArgs(price, qty, "bid"));
            }
        }

當我設法讀取價格和數量時,我只觸發 OnPricePointReceived 事件。 但是,給定價格(或沒有)可能沒有數量。 如何實施驗證,以避免基於不完整消息的錯誤?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM