2012-11-07 18 views
0

我使用的是流讀取器來得到一些網頁的HTML,但也有我想要忽略線路,如是否符合<span>  VB.net如何讓流讀取器忽略一些行?

任何建議開始? 這裏是我的功能

Public Function GetPageHTMLReaderNoPrx(ByVal address As Uri) As StreamReader 
    Dim request As HttpWebRequest 
    Dim response As HttpWebResponse = Nothing 
    Dim reader As StreamReader 

    Try 
    request = DirectCast(WebRequest.Create(address), HttpWebRequest) 
    response = DirectCast(request.GetResponse(), HttpWebResponse) 

    Select Case CType(response, Net.HttpWebResponse).StatusCode 
     Case 200 
     reader = New StreamReader(response.GetResponseStream(), Encoding.Default) 

     Case Else 
     MsgBox(CType(response, Net.HttpWebResponse).StatusCode) 
    End Select 
    Catch 
    If Not response Is Nothing Then response.Close() 
    End Try 
    Return reader 
End Function 

這是HTML的樣子

<tr>Text 
<span>show all</span> 
</tr> 
+0

你確定你正在做的是正確的?通常使用HTML,你不應該看線,而是考慮HTML標籤。 – Neolisk

+0

@Neolisk這行開頭是這樣的&nbsp,我想忽略它 – user1570048

+0

@並且它以結尾,在同一行中沒有其他標籤,它使用字符串,它工作正常,除了我想用讀者 – user1570048

回答

1

如果你堅持使用字符串,你可以做這樣的事情:

Do 
    Dim line As String = reader.ReadLine() 
    If line Is Nothing Then Exit Do 'end of stream 
    If line.StarsWith("<span>") Then Exit Do 'ignore this line 
    'otherwise do some processing here 
    '... 
Loop 

但這種方法不穩定 - 輸入HTML中的任何細微變化都可能會破壞流程。

更優雅的解決辦法是使用XElement

Dim xml = <tr>Text 
      <span>show all</span> 
      </tr> 
xml.<span>.Remove() 
MsgBox(xml.Value.Trim)