将此XML读取到Dictionary(Linq或其他什么?)的最快/最有效的方法是什么?

Mac*_*cin 6 c# 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>
Run Code Online (Sandbox Code Playgroud)

我的目标是解析价格点元素

<pricePoint>
      <price>1358.2</price>
      <quantity>20</quantity>
</pricePoint>
Run Code Online (Sandbox Code Playgroud)

到以下结构的字典:

Dictionary<double, PriceLevel> 
Run Code Online (Sandbox Code Playgroud)

价格应该是双倍,而PriceLevel是一个类

class PriceLevel
{
     int bid;
     int offer;

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


}
Run Code Online (Sandbox Code Playgroud)

根据元素,每个价格点存在(投标或报价)数量应相应地分配,即如果出价中存在价格点,则应将数量分配给出价,并将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));
                }
            }
        }
Run Code Online (Sandbox Code Playgroud)

但我正在努力将元素名称与其价值联系起来...即,我如何知道我目前正在阅读哪个投标价格点以及这是否存在于出价或报价中?谢谢你的帮助

Mic*_*ker 4

当我们使用基于事件的界面时,类似于更新中提供的界面,您将需要记住上一个启动元素事件的名称。通常,持有堆栈来跟踪事件是值得的。我可能会做类似以下的事情:

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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)