Oscar M Oscar M - 1 month ago 11
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
string
. However, the code is very large for a
string
. So that makes it impossible, because the amount
0,896903
I am searching does not exist in the
string
.

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);
}
else
{
readStream = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet));
}

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

response.Close();
readStream.Close();
}


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

L.B L.B
Answer

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']")
               .InnerText;

A Linq version is also possible

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

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>")
               .Groups[1]
               .Value;
Comments