Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4597870
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T23:19:07+00:00 2026-05-21T23:19:07+00:00

I am very new to parsing XML, and I started learning about linq which

  • 0

I am very new to parsing XML, and I started learning about linq which I think might be the best solution here. I am mostly interested in performance as the application I am creating will read stock exchange prices, which sometimes can change very rapidly.
I receive following message from the server:

<?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>

My aim is to parse price point elements

<pricePoint>
      <price>1358.2</price>
      <quantity>20</quantity>
</pricePoint>

into dictionary of the following structure:

Dictionary<double, PriceLevel> 

where price should be a double and PriceLevel is a class

class PriceLevel
{
     int bid;
     int offer;

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


}

Depending on the element, in which each price point exists (either bids or offers) quantity should be assigned accordingly, i.e. if price point exists in bids, then quantity should be assigned to bid, and 0 to offer. On the opposite, if price point exists in offers, then quantity should be assigned to offer and 0 to bid.

I hope my explanation is clear, however if you have any problems understanding it, please do not hesitate to ask for clarification in comments.
I would greatly appreciate help in solving this problem.

+++++++++++++++++++++++++++++++++++++++++
Update:

I have gone deeper into the stream I am trying to read, and it is not going to be as simple as I expected. I found out, that the stream will not always contain the whole document, therefore I will have to read it using XmlReader to process the stream on the ongoing basis. In this case, how can I read bids and offers? I have something like this:

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

but I am struggling to link element name to its value … ie, how can I know which bid price point I am currently reading and if this exists in bids or offers?
Thank you for help

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T23:19:08+00:00Added an answer on May 21, 2026 at 11:19 pm

    When are are using a event based interface, similar to the one presented in your update, you will need to remember the name of the previous start element event. Often it is worth while holding a stack to keep track of the events. I would probably do something similar to the following:

    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;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Very new to FluentNHibernate, but I'm also excited about the area. I've recently started
Hi I am very much new to xml parsing ,i am trying to parse
I am very new to parsing XML data with javascript, so please excuse me
Extended Backus–Naur Form: EBNF I'm very new to parsing concepts. Where can I get
I am new to iPhone development. I am parsing a XML URL and display
I'm new to XML parsing and want to convert XML file resulting from a
I am very new to Python and parsing data. I can pull an external
I'm very new to jQuery and I am having an issue with parsing a
Very new to JQuery and MVC and webdevelopment over all. I'm now trying to
Im very new to SQL but need to write a query to do the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.