使用C#LINQ搜索XML文件

时间:2018-02-10 18:15:57

标签: c# asp.net xml linq

我目前正在使用ASP.NET C#查看使用LINQ和XML的一些教程。我想创建一个读取xml文件的搜索页面,并在单击按钮后在网格视图中返回结果。

这是我试图搜索的XML文件:

<?xml version="1.0"?>
<catalog>
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
   </book>
   <book id="bk102">
      <author>Ralls, Kim</author>
      <title>Midnight Rain</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-12-16</publish_date>
      <description>A former architect battles corporate zombies, 
      an evil sorceress, and her own childhood to become queen 
      of the world.</description>
   </book>
</catalog>

我有一个文本框和搜索按钮:

 <asp:TextBox ID="searchtxt" runat="server" /> &nbsp; <asp:Button ID="search_btn" Text="Search" runat="server" OnClick="search_btn_Click" />

......网格视图:

<div>
            <asp:GridView ID="gvSearch" runat="server" EmptyDataText="No Results found" Width="618px" AutoGenerateColumns="false" CssClass="gridviewsearch" GridLines="None">
            <Columns>
                 <asp:TemplateField HeaderText="Keyword">
                             <ItemTemplate>
                                <%# Eval("author")%>
                            </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Results">
                             <ItemTemplate>
                                <%# Eval("title")%>
                            </ItemTemplate>
                    </asp:TemplateField>
                     <asp:TemplateField HeaderText="URL">
                             <ItemTemplate>
                                <%# Eval("genre")%>
                            </ItemTemplate>
                    </asp:TemplateField>
            </Columns>
            </asp:GridView>
        </div>

...按钮背后的代码:

  protected void search_btn_Click(object sender, EventArgs e)
    {
        //1 .create a reference of XDocument 
        XDocument xdoc = XDocument.Load("database\\books.xml");

        xdoc.Descendants("book").Select(p => new
        {
            author = p.Attribute("author").Value,
            title = p.Element("title").Value,
            genre = p.Attribute("genre").Value

        }) .OrderBy(p => p.author).ToList().ForEach(p =>
        {
            gvSearch.DataSource = xdoc;
            gvSearch.DataBind();
        });
    }

...我的问题是,如何将id:searchtxt实现到代码的后端?另外,我是否在正确的道路,设置和语法方面完成任务?

1 个答案:

答案 0 :(得分:0)

识别目录 descendants并迭代图书 elements

var term = "rain";

var query = from element in document
                .Descendants("catalog")
                .Elements("book")
            let book = new
            {
                id = element.Attribute("id").Value,
                author = element.Element("author").Value,
                title = element.Element("title").Value,
                genre = element.Element("genre").Value
            }
            where book.author.ToLower().Contains(term)
                || book.title.ToLower().Contains(term)
                || book.genre.ToLower().Contains(term)
            select book;

会给你

id|author|title|genre 
bk102|Ralls, Kim|Midnight Rain|Fantasy