【发布时间】:2022-01-03 08:37:46
【问题描述】:
我是 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>
【问题讨论】:
-
“不工作”是什么意思?你有错误吗?它是否以某种方式无法满足您的需求?它如何偏离您的预期?这些是您需要在问题中包含的关键细节。