如何在 ASP.NET 网络表单中搜索数据表单 XML

how to search data form XML in ASP.NET webforms

我是 ASP.NET 的新手。我正在 ASP.NET 网络表单中开发一个搜索应用程序,它将从 XMl 文件接收。当我在 student_name 或 ID 之类的文本框中输入并单击提交按钮时,它应该以一种很好的格式从 XML 中检索学生的数据,例如他们的荣誉。这是我制作的示例,但它不起作用。

aspx 文件

<body style="height: 171px">

<h1>Student Graduation</h1>

<form runat="server">
    Search for 
    <asp:TextBox ID="SearchTextBox" runat="server" />
    <asp:Button ID="SearchButton" Text="Search!" runat="server" OnClick="SearchButton_Click" />
</form>


<div id="student_output" runat="server"></div>

aspx.cs

  protected void Page_Load(object sender, EventArgs e)
  {
    XmlDocument xmlDoc = new XmlDocument();

    protected void Page_Load(object sender, EventArgs e)
    {

        xmlDoc.Load(Server.MapPath("student_list.xml"));

        XmlNodeList Students = xmlDoc.DocumentElement.GetElementsByTagName("Student");

        String htmlString = "";

        for (int i = 0; i < Students.Count; i++)
        {
            XmlNode Student = Students[i];

            htmlString += "<h2>" + Student.SelectSingleNode("Student_Name").InnerText + "</h2>";

            htmlString += "<p>";

            htmlString += Student.SelectSingleNode("ID").InnerText + ", ";

            htmlString += Student.SelectSingleNode("Honours").InnerText + ", ";

            htmlString += Student.SelectSingleNode("Programme").InnerText + ", ";

            htmlString += Student.SelectSingleNode("Book_Price").InnerText + ". ";

            htmlString += "</p>";

        }
        student_output.InnerHtml = htmlString;
    }

    protected void SearchButton_Click(object sender, EventArgs e)
    {
        String searchKey = SearchTextBox.Text;

        XmlNode Student = xmlDoc.SelectSingleNode("//Student[Student_Name='" + searchKey + "']");

        if (Student != null)
        {
            string htmlString = "";

            htmlString += "<h2>" + Student.SelectSingleNode("Student_Name").InnerText + "</h2>";

            htmlString += "<p>";

            htmlString += Student.SelectSingleNode("ID").InnerText + ", ";

            htmlString += Student.SelectSingleNode("Honours").InnerText + ", ";

            htmlString += Student.SelectSingleNode("Programme").InnerText + ". ";

            htmlString += Student.SelectSingleNode("Book_Price").InnerText + ". ";

            htmlString += "</p>";

            student_output.InnerHtml = htmlString;
        }
    }
}

}

XML 文件

<Graduate>

<Student>
    <ID> 01944422</ID>
    <Student_Name>Peter Parker</Student_Name>
    <Honours> First Class </Honours>
    <Book_Price>Yes</Book_Price>
    <Programme>Comp. Science</Programme>
</Student>

<Student>
    <ID>01923455</ID>
    <Student_Name>Bryan Adam</Student_Name>
    <Honours>Second class</Honours>
    <Book_Price>No</Book_Price>
    <Programme>Mathematics</Programme>
</Student>

<Student>
    <ID>01952345</ID>
    <Student_Name>Maggie Fong</Student_Name>
    <Honours>First class</Honours>
    <Book_Price>Yes</Book_Price>
    <Programme>Accounting</Programme>
</Student>

<Student>
    <ID>01998745</ID>
    <Student_Name>Melissa Teh</Student_Name>
    <Honours>First class</Honours>
    <Book_Price>Yes</Book_Price>
    <Programme>Finance</Programme>
</Student>


<Student>
    <ID>01899888</ID>
    <Student_Name>Ahmad bin Suhail</Student_Name>
    <Honours>Second class</Honours>
    <Book_Price>No</Book_Price>
    <Programme>Engineering</Programme>
</Student>

<Student>
    <ID>01900847</ID>
    <Student_Name>Lechumanan a/l Vicky</Student_Name>
    <Honours>Third class</Honours>
    <Book_Price>No</Book_Price>
    <Programme>Comp. Science</Programme>
</Student>

<Student>
    <ID>04503967</ID>
    <Student_Name>Soo Tong Wei</Student_Name>
    <Honours>Third class</Honours>
    <Book_Price>No</Book_Price>
    <Programme>Mathematics</Programme>
</Student>

将 xml 转换为数据集,然后一切都变得轻松起来。

所以,这个标记:

    <div>
        <h1>Student Graduation</h1>
            Search for 
            <asp:TextBox ID="SearchTextBox" runat="server" />
            <asp:Button ID="SearchButton" Text="Search!" runat="server" OnClick="SearchButton_Click" 
                CssClass="btn" style="margin-left:15px"       />

        <br />
        <asp:GridView ID="GridView1" runat="server" CssClass="table" Width="40%">
        </asp:GridView>
    </div>

好的,然后是这段代码:

   protected void SearchButton_Click(object sender, EventArgs e)
    {
        string sFile  = @"C:\Test7\sData.txt";
        DataSet MyData = new DataSet();
        MyData.ReadXml(sFile);

        // if blank, then show all data
        DataTable MyTable = MyData.Tables[0];
        if (SearchTextBox.Text != "")
        {
            string s = SearchTextBox.Text;
            // user entered somthing
            // if text, then match on stuent name

            if (s.All(char.IsNumber))
            {
                // number, lets filter by ID
                MyTable.DefaultView.RowFilter = "ID = " + s;
            }
            else
            {
                // search Student name - partial match
                MyTable.DefaultView.RowFilter = "Student_Name like '%" + s + "%'";
            }
        }

        GridView1.DataSource = MyTable;
        GridView1.DataBind();
    }

所以,如果我们什么都不输入,那么我们会显示所有内容并看到:

我使用通配符搜索名称 - 我可以假设只是匹配开头,但你可以说搜索“B”,你会得到这个:

当然,如果你输入数字,那我按ID搜索,这样说:

我想我们应该在 GV 中添加一个“无数据行,这样说:

        <asp:GridView ID="GridView1" runat="server" CssClass="table" Width="40%">
            <EmptyDataTemplate>
                <h2>No data found</h2>
            </EmptyDataTemplate>
        </asp:GridView>

所以,现在对于失败的搜索,我们会得到这个: