【问题标题】:Using C# LINQ to Search an XML file使用 C# LINQ 搜索 XML 文件
【发布时间】:2018-02-10 18:15:56
【问题描述】:

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

这是我正在尝试搜索的 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 实现到代码的后端?此外,在完成任务方面,我是否走在正确的道路、设置和语法方面?

【问题讨论】:

  • 你不需要SelectOrderBy之间的ToList,老实说,我建议不要养成使用ToList的习惯,这样你就可以使用@987654329 @。相反,只需将结果分配给变量并使用常规 foreach 代替(它可以节省创建不需要的中间列表)
  • xdoc.Descendants("book") 试试你的代码而不是catalog
  • 感谢您的建议 .. 当我单击按钮时,我在以下行得到一个对象引用未设置为对象错误的实例:xdoc.Descendants("book").Select(p => new ...我不确定如何将searchtxt(文本框的ID)绑定到后端。您有什么建议吗?

标签: c# asp.net xml linq


【解决方案1】:

识别 目录 descendants 并遍历 book 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

【讨论】:

  • ……还是,文本框的ID值:searchtxt,如何绑定到后端代码中?我相信这就是产生对象引用错误的原因
  • 在您的代码中,您将authorgenre 视为一个属性,而它最有可能是元素
  • 用于将搜索值传递给底层的点击处理程序,这对于 asp.net 来说是非常具体的,因为我没有接触任何这些东西已经太久了,但我猜是命令参数应该做的伎俩stackoverflow.com/questions/30461086/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-10-22
  • 1970-01-01
  • 2019-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-02
相关资源
最近更新 更多