什么是最快/最有效的方式来读取这个XML到字典(Linq或其他?)

本文关键字:字典 Linq XML 其他 有效 读取 方式 什么 | 更新日期: 2023-09-27 17:54:30

我对解析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));
                }
            }
        }

,但我正在努力链接元素名称到它的值…例如,我如何知道我目前正在阅读的投标价格点,以及这是否存在于投标或报价中?谢谢你的帮助

什么是最快/最有效的方式来读取这个XML到字典(Linq或其他?)

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

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将它们分组为bid和ask。

一旦你做到了这一点,在展示你的数据应该没有问题,因为你已经得到了价格,并将它们分为出价和要价

我设法得到这样的东西:

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事件时,我设法读取价格和数量。然而,在给定的价格下,有可能没有(或没有)数量。如何实现验证,以避免基于不完整消息的错误?