閲覧総計:2147 (本日:1 昨日:0)
XMLデータをLINQ to XMLの機能(Where句)を用い、検索し、結果をGridViewに表示した。
09/09/06
↓このページを参考にした。
http://itpro.nikkeibp.co.jp/article/COLUMN/20080604/306335/?ST=develop&P=1
開発環境:VWD2008
サーバ:Windows Server2003 + .NET Framework3.5
【機能】検索キーワードを含む情報のみ抽出し、結果をGridViewに表示する。
※XMLデータは↓のリスト1の udons_sampleData.xml を App_Dataフォルダ内に置く。
http://itpro.nikkeibp.co.jp/article/COLUMN/20080312/296047/?ST=develop
【稼働画面】
【SearchLinqXml.aspx】[3.5]
<%@ Page Language="VB" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) 'xmlデータをXElementクラスで読み込む Dim xmldoc = XElement.Load(Server.MapPath("~/App_Data/udons_sampleData.xml")) '全件をGridViewに表示 Dim list = From c In xmldoc.Descendants("商品") _ Select _ 品番 = c.Element("品番").Value, _ 品名 = c.Element("品名").Value, _ セット内容 = c.Element("セット内容").Value, _ 価格 = c.Element("価格").Value GridView1.DataSource = list GridView1.DataBind() End Sub Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) 'xmlデータをXElementクラスで読み込む Dim xmldoc = XElement.Load(Server.MapPath("~/App_Data/udons_sampleData.xml")) '検索:Where句で検索 Dim list = From c In xmldoc.Descendants("商品") _ Where c.Element("セット内容").Value.Contains(TextBox1.Text) _ Select _ 品番 = c.Element("品番").Value, _ 品名 = c.Element("品名").Value, _ セット内容 = c.Element("セット内容").Value, _ 価格 = c.Element("価格").Value '結果をGridViewに表示 GridView1.DataSource = list GridView1.DataBind() End Sub </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>検索(LINQ to XML) </title> </head> <body> <form id="form1" runat="server"> <div> 検索(LINQ to XML) 09/09/06<br /> 「セット内容」に「検索キーワード」を含むメニューを抽出<br /> <br /> 検索キーワード:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:Button ID="Button1" runat="server" Height="21px" Text="検索" Width="65px" onclick="Button1_Click" /> <br /> <br /> <asp:GridView ID="GridView1" runat="server"> </asp:GridView> </div> </form> </body> </html>
【参考にしたページ】
1.部分一致検索:1.指定文字列を含む特定要素の抽出 08/06/10
http://itpro.nikkeibp.co.jp/article/COLUMN/20080604/306335/?ST=develop&P=1