博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
[索引页]
[源码下载]


步步为营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>
        
&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();
    }

}



OK
[源码下载]