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