Linq To XML
来源:互联网 发布:淘宝咋设置选购热点 编辑:程序博客网 时间:2024/05/21 09:08
Sample.xml
<?xml version="1.0" encoding="utf-8"?><root> <person name="webabcd" age="27" salary="33" /></root>
html:
<html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server"> <title></title></head><body> <form id="form1" 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> </form></body></html>
后台代码:
public partial class WebForm2 : 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(); } }
- 使用LINQ to XML
- LINQ TO XML
- LINQ TO XML学习
- linq to xml demo
- LINQ to XML简介
- LINQ to XML
- linq to xml入门
- LINQ to XML
- LINQ TO Xml【转载】
- LINQ to XML
- LINQ TO XML
- LINQ to XML
- LINQ to XML
- 学习心得LINQ to XML
- Linq to XML批注
- Linq to XML轴
- Linq to XML事件
- Linq To Xml
- DB2常用语句集萃
- android学习笔记一
- Flash Builder 4.6与php通信时,返回值为乱码的解决方案
- insight的安装
- ADSPBF531 SPI Slave Boot with AT91SAM9260引导调试笔记
- Linq To XML
- 使用 Java Native Interface 的最佳实践
- ASP.NET备份数据与还原数据,解决数据库正在使用的问题
- android自动化测试-monkey使用举例(2)
- mysql常用语句
- Lesson 13: 文档串行化
- 经常使用的命令
- #define用法
- Lesson14 网络编程