.NET 3.5(14) - XLINQ(LINQ to XML)之针对XML文件的添加、查询、更新和删除

本文通过一个XML文件示例,介绍了如何使用XLINQ(LINQ to XML)进行添加、查询、更新和删除操作。详细展示了在ASP.NET中结合C#语言,实现对XML数据的动态操作。

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

步步为营VS 2008 + .NET 3.5(14) - XLINQ(LINQ to XML)之针对XML文件的添加、查询、更新和删除



作者:webabcd


介绍
以某一XML文件为例,XLINQ(LINQ to XML)之针对XML文件的添加操作、查询操作、更新操作和删除操作


示例
Sample.xml

<? xml version ="1.0" encoding ="utf-8" ?>
< root >
     < person name ="webabcd" age ="27" salary ="33" />
</ root >

 

Sample.aspx

<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Sample.aspx.cs"
        Inherits="LINQ_XLINQ_Sample" Title="针对XML文件的添加、查询、更新和删除" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
        <p>
                姓名:<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
                   年龄:<asp:TextBox ID="txtAge" runat="server"></asp:TextBox>
                   薪水:<asp:TextBox ID="txtSalary" runat="server"></asp:TextBox>
                <asp:Button ID="btnAdd" runat="server" Text="添加" OnClick="btnAdd_Click" />
        </p>
        <asp:GridView ID="gvPerson" runat="server" DataKeyNames="name" OnSelectedIndexChanged="gvPerson_SelectedIndexChanged"
                OnRowDeleting="gvPerson_RowDeleting" OnRowCancelingEdit="gvPerson_RowCancelingEdit"
                OnRowEditing="gvPerson_RowEditing" OnRowUpdating="gvPerson_RowUpdating">
                <Columns>
                        <asp:CommandField ShowSelectButton="True" ShowEditButton="True" ShowDeleteButton="True">
                        </asp:CommandField>
                </Columns>
        </asp:GridView>
        <br />
        <asp:DetailsView ID="dvPerson" runat="server" DataKeyNames="name">
        </asp:DetailsView>
</asp:Content>

 


Sample.aspx.cs

InBlock.gif using System;
InBlock.gif using System.Data;
InBlock.gif using System.Configuration;
InBlock.gif using System.Collections;
InBlock.gif using System.Linq;
InBlock.gif using System.Web;
InBlock.gif using System.Web.Security;
InBlock.gif using System.Web.UI;
InBlock.gif using System.Web.UI.WebControls;
InBlock.gif using System.Web.UI.WebControls.WebParts;
InBlock.gif using System.Web.UI.HtmlControls;
InBlock.gif using System.Xml.Linq;
InBlock.gif
InBlock.gif public partial class LINQ_XLINQ_Sample : System.Web.UI.Page
InBlock.gif{
InBlock.gif         protected void Page_Load( object sender, EventArgs e)
InBlock.gif        {
InBlock.gif                 if (!Page.IsPostBack)
InBlock.gif                {
InBlock.gif                        BindPerson();
InBlock.gif                }
InBlock.gif        }
InBlock.gif
InBlock.gif         private void BindPerson()
InBlock.gif        {
InBlock.gif                 // 加载指定的xml文件
InBlock.gif                XDocument xml = XDocument.Load(Server.MapPath( "Sample.xml"));
InBlock.gif
InBlock.gif                 // 使用查询语法获取Person集合
InBlock.gif                var persons = from p in xml.Root.Elements( "person")
InBlock.gif                                            select new
InBlock.gif                                            {
InBlock.gif                                                    name = p.Attribute( "name").Value,
InBlock.gif                                                    age = p.Attribute( "age").Value,
InBlock.gif                                                    salary = p.Attribute( "salary").Value
InBlock.gif                                            };
InBlock.gif
InBlock.gif                gvPerson.DataSource = persons;
InBlock.gif                gvPerson.DataBind();
InBlock.gif        }
InBlock.gif
InBlock.gif         protected void btnAdd_Click( object sender, EventArgs e)
InBlock.gif        {
InBlock.gif                 // 加载指定的xml文件
InBlock.gif                XDocument xml = XDocument.Load(Server.MapPath( "Sample.xml"));
InBlock.gif
InBlock.gif                 // 创建需要新增的XElement对象
InBlock.gif                XElement person = new XElement(
InBlock.gif                         "person",
InBlock.gif                         new XAttribute( "name", txtName.Text),
InBlock.gif                         new XAttribute( "age", txtAge.Text),
InBlock.gif                         new XAttribute( "salary", txtSalary.Text));
InBlock.gif
InBlock.gif                 // 添加需要新增的XElement对象
InBlock.gif                xml.Root.Add(person);
InBlock.gif                
InBlock.gif                 // 保存xml
InBlock.gif                xml.Save(Server.MapPath( "Sample.xml"));
InBlock.gif
InBlock.gif                gvPerson.EditIndex = -1;
InBlock.gif                BindPerson();
InBlock.gif        }
InBlock.gif
InBlock.gif         protected void gvPerson_SelectedIndexChanged( object sender, EventArgs e)
InBlock.gif        {
InBlock.gif                 // 加载指定的xml文件
InBlock.gif                XDocument xml = XDocument.Load(Server.MapPath( "Sample.xml"));
InBlock.gif
InBlock.gif                 // 使用查询语法获取指定的Person集合
InBlock.gif                var persons = from p in xml.Root.Elements( "person")
InBlock.gif                                            where p.Attribute( "name").Value == gvPerson.SelectedValue.ToString()
InBlock.gif                                            select new
InBlock.gif                                            {
InBlock.gif                                                    name = p.Attribute( "name").Value,
InBlock.gif                                                    age = p.Attribute( "age").Value,
InBlock.gif                                                    salary = p.Attribute( "salary").Value
InBlock.gif                                            };
InBlock.gif
InBlock.gif                dvPerson.DataSource = persons;
InBlock.gif                dvPerson.DataBind();
InBlock.gif        }
InBlock.gif
InBlock.gif         protected void gvPerson_RowDeleting( object sender, GridViewDeleteEventArgs e)
InBlock.gif        {
InBlock.gif                 // 加载指定的xml文件
InBlock.gif                XDocument xml = XDocument.Load(Server.MapPath( "Sample.xml"));
InBlock.gif
InBlock.gif                 // 使用查询语法获取指定的Person集合
InBlock.gif                var persons = from p in xml.Root.Elements( "person")
InBlock.gif                                            where p.Attribute( "name").Value == gvPerson.DataKeys[e.RowIndex].Value.ToString()
InBlock.gif                                            select p;
InBlock.gif
InBlock.gif                 // 删除指定的XElement对象
InBlock.gif                persons.Remove();
InBlock.gif
InBlock.gif                 // 保存xml
InBlock.gif                xml.Save(Server.MapPath( "Sample.xml"));
InBlock.gif
InBlock.gif                gvPerson.EditIndex = -1;
InBlock.gif                BindPerson();
InBlock.gif        }
InBlock.gif
InBlock.gif         protected void gvPerson_RowUpdating( object sender, GridViewUpdateEventArgs e)
InBlock.gif        {
InBlock.gif                 // 加载指定的xml文件
InBlock.gif                XDocument xml = XDocument.Load(Server.MapPath( "Sample.xml"));
InBlock.gif
InBlock.gif                 // 使用查询语法获取指定的Person集合
InBlock.gif                var persons = from p in xml.Root.Elements( "person")
InBlock.gif                                            where p.Attribute( "name").Value == gvPerson.DataKeys[e.RowIndex].Value.ToString()
InBlock.gif                                            select p;
InBlock.gif
InBlock.gif                 // 更新指定的XElement对象
InBlock.gif                 foreach (XElement xe in persons)
InBlock.gif                {
InBlock.gif                        xe.SetAttributeValue( "age", ((TextBox)gvPerson.Rows[e.RowIndex].Cells[2].Controls[0]).Text);
InBlock.gif                        xe.SetAttributeValue( "salary", ((TextBox)gvPerson.Rows[e.RowIndex].Cells[3].Controls[0]).Text);
InBlock.gif                }
InBlock.gif
InBlock.gif                 // 保存xml
InBlock.gif                xml.Save(Server.MapPath( "Sample.xml"));
InBlock.gif
InBlock.gif                gvPerson.EditIndex = -1;
InBlock.gif                BindPerson();
InBlock.gif        }
InBlock.gif
InBlock.gif         protected void gvPerson_RowEditing( object sender, GridViewEditEventArgs e)
InBlock.gif        {
InBlock.gif                gvPerson.EditIndex = e.NewEditIndex;
InBlock.gif                BindPerson();
InBlock.gif        }
InBlock.gif
InBlock.gif         protected void gvPerson_RowCancelingEdit( object sender, GridViewCancelEditEventArgs e)
InBlock.gif        {
InBlock.gif                gvPerson.EditIndex = -1;
InBlock.gif                BindPerson();
InBlock.gif        }
InBlock.gif}

 

 

本文出自 “webabcd” 博客,请务必保留此出处http://webabcd.blog.51cto.com/1787395/345020

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值