1

I am trying to log only 3rd line (or lines 1 to 3 if logging just one line is not possible) from a webResposne.

here is a snippet of the code that I am using for now.

StreamReader read = new StreamReader(myHttpWebResponse.GetResponseStream(), System.Text.Encoding.UTF8);
        String result = read.ReadToEnd();
        Log("Access", Server.HtmlEncode(result), "Success");

I am getting the following output

<html>
<head>
    <title>Access is Granted.</title>
    <style>
     body {font-family:"Verdana";font-weight:normal;font-size: .7em;color:black;} 
     p {font-family:"Verdana";font-weight:normal;color:black;margin-top: -5px}
     b {font-family:"Verdana";font-weight:bold;color:black;margin-top: -5px}
     H1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red }
     H2 { font-family:"Verdana";font-weight:normal;font-size:14pt;color:maroon }
...

and so on.

I would just like to log "(title>Access is Granted.(/title>" and not print anything else (or anything after that line).

How would I go about doing that?

Thank you

007
  • 2,136
  • 4
  • 26
  • 46
  • Have you tried writing a Regex for your task? If so, are you having issues with it? – AdamV Jan 28 '13 at 16:51
  • assuming this question is "what is the name of StreamReader.ReadLine method and if it is ok to call it 3 times ignoring result first 2 times" - possible duplicate of [How to skip first line while reading csv using streamreader](http://stackoverflow.com/questions/9093223/how-to-skip-first-line-while-reading-csv-using-streamreader) - – Alexei Levenkov Jan 28 '13 at 16:52
  • 4
    Are you sure you only want line 3 or is it the tag you're going after specifically? –  Jan 28 '13 at 16:52

6 Answers6

1

If you need read a specific line instead of using ReadToEnd you should look into using ReadLine, then you should be able to count the number of lines read to know when you've gotten to the line(s) you need.

JG in SD
  • 5,427
  • 3
  • 34
  • 46
1

You could read all lines into an array so you can refer to a specific line via index.

  • 1
    If you read in all lines at once you can use [`File.ReadLines`](http://msdn.microsoft.com/en-us/library/dd383503.aspx), but that won't work with streams, which looks like the OP is reading in. – JG in SD Jan 28 '13 at 16:57
  • If you provide an answer that refers to a method, provide an example of the method like @JGinSD. Not sure if reading all the lines is worth it to get three lines deep in a file. – ChiefTwoPencils Feb 13 '13 at 23:46
1

Build extension method:

public static IEnumerable<string> ReadLines(this StreamReader reader)
{
     yield return reader.ReadLine();
}

Then you can use LINQ to select any line you want, example below is to select the third line:

 var result  = streamReader.ReadLines()
                           .ElementAtOrDefault(2);

You still take advantage of deferred execution on this way

cuongle
  • 74,024
  • 28
  • 151
  • 206
1

Regex will do the trick. Simple example:

string test = @"<html>\n<head>\n<title>Access is Granted.</title>\n<style>...";
string output = Regex.Match(test, "<title>.*</title>").Value;
Chris_LE
  • 31
  • 2
0

Use HtmlAgilityPack.

Run the response through it and extract the line(s) you need.

Plain and simple

lboshuizen
  • 2,746
  • 17
  • 20
0

How about using an XmlReader to read the exact value you want from the HTML document? Since XmlReader is streaming you won't have to bother reading the entire document as with the array method, and it will automatically parse it for you. This is safer than relying on the <title> tag being on a certain line.

using(var reader = XmlReader.Create(myHttpWebResponse.GetResponseStream()))
{
    reader.ReadToDescendant("title");
    var result = "<title>" + reader.ReadElementString() + "</title>";
    Log("Access", Server.HtmlEncode(result), "Success");
}
luksan
  • 7,661
  • 3
  • 36
  • 38
  • `XmlReader` may likely fail trying to read in HTML, as HTML is not necessarily XML – JG in SD Jan 28 '13 at 17:02
  • Yeah, I tested it with the given HTML, seemed to work. Only the first 3 lines need to be valid XML, since XmlReader parses lazily. – luksan Jan 28 '13 at 17:03