一個使用VS2005,.Net FrameWork 2.0在XML元素中進行模糊查詢的DEMO,希望對你用..
我不能保證這個地址(http://msdn.microsoft.com/globalrss/zh-cn/global-MSDN-zh-cn.XML)始終有效,如果該地址
不可用,可以使用任何使用rss標准的XML做為替代.
index.htm
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xHtml1-transitional.dtd">
<html XMLns="http://www.w3.org/1999/xHtml" >
<head>
<meta http-equiv="Content-Type" content="text/Html; charset=utf-8" />
<title>Untitled Page</title>
</head>
<body>
<form method="post" action="searchByStream.ASPx" >
<input id="txtQ" name="txtQ" type="text" /> <input id="SubmitStream" type="submit" value="搜索流" />
</form>
</body>
</Html>
searchByStream.ASPx<%...@ Page EnableVIEwState="false" Language="vb" AutoEventWireup="false" CodeBehind="searchByStream.ASPx.vb" Inherits="testWebForm.searchByStream" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xHtml1-transitional.dtd">
<html XMLns="http://www.w3.org/1999/xHtml" >
<head runat="server">
<title>無標題頁</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<ASP:Repeater ID="rep" runat="server">
<HeaderTemplate><ul></HeaderTemplate>
<ItemTemplate>
<li runat="server" id="msdnItem"><a href='<%# xpath("link") %>'><%...#XPath("title")%></a></li>
</ItemTemplate>
<FooterTemplate></ul></FooterTemplate>
</ASP:Repeater>
</div>
</form>
</body>
</Html>
search.ASPx.vbPublic Partial Class searchByStreamClass searchByStream
Inherits System.Web.UI.Page
Private Const url As String = "http://msdn.microsoft.com/globalrss/zh-cn/global-MSDN-zh-cn.XML"
Protected Sub Page_Load()Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
If Not String.IsNullOrEmpty(Request.Form("txtQ")) Then
Call run()
End If
End If
End Sub
Private Sub run()Sub run()
rep.DataSource = getDataSource
rep.DataBind()
End Sub
Public Function getDataSource()Function getDataSource()
Using wc As New System.Net.WebClIEnt
With wc
Using sr As System.IO.Stream = .OpenRead(url)
Dim doc As New System.Xml.XMLDocument
doc.Load(sr)
Dim xpath As String = "/rss/channel/item[contains(title,'" & HttpUtility.HtmlEncode(Request.Form("txtQ")) & "')]"
Return doc.SelectNodes(xpath)
End Using
End With
End Using
End Function
Private Sub rep_ItemDataBound()Sub rep_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rep.ItemDataBound
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
DirectCast(e.Item.FindControl("msdnItem"), HtmlControls.HtmlContainerControl).InnerHtml = DirectCast(e.Item.FindControl("msdnItem"), HtmlControls.HtmlContainerControl).InnerText.Replace(HttpUtility.HtmlEncode(Request.Form("txtQ")), "<span background-color:yellow"">" & HttpUtility.HtmlEncode(Request.Form("txtQ")) & "</span>")
End If
End Sub
End Class