<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="xmldemo2.aspx.cs" Inherits="Ado_upper.xmldemo2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr><td colspan=2>书名<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" Text="显示详细信息" onclick="Button1_Click" /></td></tr>
<tr><td>作者</td> <td><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td></tr>
<tr><td>作者</td> <td><asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></td></tr>
<tr><td>作者</td> <td><asp:TextBox ID="TextBox3" runat="server"></asp:TextBox></td></tr>
<tr><td>作者</td> <td><asp:TextBox ID="TextBox4" runat="server"></asp:TextBox></td></tr>
<tr><td>作者</td> <td><asp:TextBox ID="TextBox5" runat="server"></asp:TextBox></td></tr>
<tr><td>作者</td> <td><asp:TextBox ID="TextBox6" runat="server"></asp:TextBox></td></tr>
</table>
</div>
</form>
</body>
</html>
apx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
namespace Ado_upper
{
public partial class xmldemo2 : System.Web.UI.Page
{
XmlDocument xdoc;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
XmlDocument xdoc = new XmlDocument();
xdoc.Load(Server.MapPath("books.xml"));//声明一个xml文档实力
XmlNodeList list = xdoc.GetElementsByTagName("name");
foreach (XmlNode node in list)
{
this.DropDownList1.Items.Add(node.InnerText);
}
Session["doc"] = xdoc;
}
else
{
xdoc = Session["doc"] as XmlDocument;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
XmlNode node= xdoc.DocumentElement.SelectSingleNode("book[name='" + DropDownList1.Text + "']");
foreach (XmlNode item in node.ChildNodes)
{
if (item.LocalName == "author")
{
this.TextBox1.Text = item.InnerText;
}
if (item.LocalName == "publisher")
{
this.TextBox2.Text = item.InnerText;
}
if (item.LocalName == "date")
{
this.TextBox3.Text = item.InnerText;
}
if (item.LocalName == "name")
{
this.TextBox4.Text = item.InnerText;
}
if (item.LocalName == "isbn")
{
this.TextBox5.Text = item.InnerText;
}
if (item.LocalName == "price")
{
this.TextBox6.Text = item.InnerText;
}
}
}
}
}
内容上划红圈的地方请忽略。代码改一下就可以了。