Skip to main content
 首页 » 编程设计

c#之StreamReader C#之只读取特定行

2024年02月20日12mq0036

我正在尝试从 webResposne 中仅记录第 3 行(如果仅记录一行是不可能的,则为第 1 到第 3 行)。

这是我现在使用的代码片段。

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

我得到以下输出

<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 } 
... 

等等。

我只想记录“(title>Access is Granted.(/title>”) 并且不打印任何其他内容(或该行之后的任何内容)。

我该怎么做呢?

谢谢

请您参考如下方法:

您可以将所有行读入一个数组,这样您就可以通过索引引用特定行。