Oscar M Oscar M - 5 months ago 28
HTML Question

How can I store a part of a very large HTML stream?

I have to get the HTML code of a web and after that to find this class:

<span class='uccResultAmount'>0,896903</span>

I have tried with Regular-Expressions.
And also with Streams, I mean, storing the whole HTML code in a
. However, the code is very large for a
. So that makes it impossible, because the amount
I am searching does not exist in the

Is there any way to only read a little block of the Stream?

A part of the method:

public static string getValue()
string data = "not found";
string urlAddress = "http://www.xe.com/es/currencyconverter/convert/?Amount=1&From=USD&To=EUR";

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlAddress);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

if (response.StatusCode == HttpStatusCode.OK)
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = null;

if (response.CharacterSet == null)
readStream = new StreamReader(receiveStream);
readStream = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet));

data = readStream.ReadToEnd(); // the string in which I should search for the amount


If you find an easier way to fix my problem let me know it.


I would use HtmlAgilityPack and Xpath

var web = new HtmlAgilityPack.HtmlWeb();
var doc = web.Load("http://www.xe.com/es/currencyconverter/convert/?Amount=1&From=USD&To=EUR");
var value = doc.DocumentNode.SelectSingleNode("//span[@class='uccResultAmount']")

A Linq version is also possible

var value = doc.DocumentNode.Descendants("span")
            .Where(s => s.Attributes["class"] != null && s.Attributes["class"].Value == "uccResultAmount")

Don't use this. Just to show

But the problem is that this html code does not fit in a single string

is not correct

string html = new WebClient().DownloadString("http://www.xe.com/es/currencyconverter/convert/?Amount=1&From=USD&To=EUR");
var val = Regex.Match(html, @"<span[^>]+?class='uccResultAmount'>(.+?)</span>")