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

介绍
以某一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 >
       
&nbsp;&nbsp; 年龄: < asp:TextBox ID ="txtAge" runat ="server" ></ asp:TextBox >
       
&nbsp;&nbsp; 薪水: < 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
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

public partial class LINQ_XLINQ_Sample : System.Web.UI.Page
{
   
protected void Page_Load(object sender, EventArgs e)
   
{
       
if (!Page.IsPostBack)
       
{
            BindPerson();
        }

    }


   
private void BindPerson()
   
{
       
// 加载指定的xml文件
        XDocument xml = XDocument.Load(Server.MapPath("Sample.xml"));

       
// 使用查询语法获取Person集合
        var persons = from p in xml.Root.Elements("person")
                      select
new
                     
{
                          name
= p.Attribute("name").Value,
                          age
= p.Attribute("age").Value,
                          salary
= p.Attribute("salary").Value
                      }
;

        gvPerson.DataSource
= persons;
        gvPerson.DataBind();
    }


   
protected void btnAdd_Click(object sender, EventArgs e)
   
{
       
// 加载指定的xml文件
        XDocument xml = XDocument.Load(Server.MapPath("Sample.xml"));

       
// 创建需要新增的XElement对象
        XElement person = new XElement(
           
"person",
           
new XAttribute("name", txtName.Text),
           
new XAttribute("age", txtAge.Text),
           
new XAttribute("salary", txtSalary.Text));

       
// 添加需要新增的XElement对象
        xml.Root.Add(person);
       
       
// 保存xml
        xml.Save(Server.MapPath("Sample.xml"));

        gvPerson.EditIndex
= -1;
        BindPerson();
    }


   
protected void gvPerson_SelectedIndexChanged(object sender, EventArgs e)
   
{
       
// 加载指定的xml文件
        XDocument xml = XDocument.Load(Server.MapPath("Sample.xml"));

       
// 使用查询语法获取指定的Person集合
        var persons = from p in xml.Root.Elements("person")
                      where p.Attribute(
"name").Value == gvPerson.SelectedValue.ToString()
                      select
new
                     
{
                          name
= p.Attribute("name").Value,
                          age
= p.Attribute("age").Value,
                          salary
= p.Attribute("salary").Value
                      }
;

        dvPerson.DataSource
= persons;
        dvPerson.DataBind();
    }


   
protected void gvPerson_RowDeleting(object sender, GridViewDeleteEventArgs e)
   
{
       
// 加载指定的xml文件
        XDocument xml = XDocument.Load(Server.MapPath("Sample.xml"));

       
// 使用查询语法获取指定的Person集合
        var persons = from p in xml.Root.Elements("person")
                      where p.Attribute(
"name").Value == gvPerson.DataKeys[e.RowIndex].Value.ToString()
                      select p;

       
// 删除指定的XElement对象
        persons.Remove();

       
// 保存xml
        xml.Save(Server.MapPath("Sample.xml"));

        gvPerson.EditIndex
= -1;
        BindPerson();
    }


   
protected void gvPerson_RowUpdating(object sender, GridViewUpdateEventArgs e)
   
{
       
// 加载指定的xml文件
        XDocument xml = XDocument.Load(Server.MapPath("Sample.xml"));

       
// 使用查询语法获取指定的Person集合
        var persons = from p in xml.Root.Elements("person")
                      where p.Attribute(
"name").Value == gvPerson.DataKeys[e.RowIndex].Value.ToString()
                      select p;

       
// 更新指定的XElement对象
        foreach (XElement xe in persons)
       
{
            xe.SetAttributeValue(
"age", ((TextBox)gvPerson.Rows[e.RowIndex].Cells[2].Controls[0]).Text);
            xe.SetAttributeValue(
"salary", ((TextBox)gvPerson.Rows[e.RowIndex].Cells[3].Controls[0]).Text);
        }


       
// 保存xml
        xml.Save(Server.MapPath("Sample.xml"));

        gvPerson.EditIndex
= -1;
        BindPerson();
    }


   
protected void gvPerson_RowEditing(object sender, GridViewEditEventArgs e)
   
{
        gvPerson.EditIndex
= e.NewEditIndex;
        BindPerson();
    }


   
protected void gvPerson_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
   
{
        gvPerson.EditIndex
= -1;
        BindPerson();
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值