c#读取xml内容保定到dropdownlist

本文介绍了一个使用ASP.NET进行XML数据绑定的具体实例。该示例通过下拉列表选择书籍名称,并显示与该书相关的详细信息,如作者、出版社等。此应用展示了如何加载XML文件并将其数据绑定到Web页面元素上。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<%@ 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;
                }
            }
        }
    }
}

内容上划红圈的地方请忽略。代码改一下就可以了。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值