2016-10-27 61 views
0

基本上我创建了一个webform让用户添加,编辑和删除现有的xml文件的元素(或记录),让用户将他/她的数据值写入文本框。当我创建一个按钮来编辑/更新元素值时,现在的问题就来了。每当这个按钮被点击时,用户将被要求输入已经存在的学生元素的ID属性值然后将在我的linq查询代码中进行检查,就好像该ID存在或不存在一样,并且如果它存在所有其他嵌套元素,则将为该元素将值打印到所有文本框中。 下面是我的全部代码和XML文件也:我想更新xml元素的属性,它的嵌套元素值在C#中

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="xmlCRUD.aspx.cs" Inherits="WebApplication5.xmlCRUD" %> 
 

 
<!DOCTYPE html> 
 

 
<html xmlns="http://www.w3.org/1999/xhtml"> 
 
<head runat="server"> 
 
    <title></title> 
 
</head> 
 
<body> 
 
    <form id="form1" runat="server"> 
 
    <div> 
 
    
 
     &nbsp;&nbsp;&nbsp; 
 
     <br /> 
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Student-ID : 
 
     <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 
 
     <br /> 
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
 
     <br /> 
 
&nbsp;&nbsp;&nbsp;&nbsp; Student Name: 
 
     <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> 
 
     <br /> 
 
     <br /> 
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Gender :<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox> 
 
     <br /> 
 
     <br /> 
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Total Marks : 
 
     <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox> 
 
     <br /> 
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
 
     <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Add Student" /> 
 
     <br /> 
 
     <br /> 
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
 
     <asp:Button ID="Button2" runat="server" Text="Edit Data" OnClick="Button2_Click" /> 
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
 
     <asp:TextBox ID="TextBox5" runat="server" Placeholder="Enter student-ID" Visible="False"></asp:TextBox> 
 
     <br /> 
 
&nbsp;&nbsp; 
 
     <br /> 
 
     <br /> 
 
     <br /> 
 
     <br /> 
 
    
 
    </div> 
 
    </form> 
 
</body> 
 
</html>

using System; 
 
using System.Collections.Generic; 
 
using System.Linq; 
 
using System.Web; 
 
using System.Web.UI; 
 
using System.Web.UI.WebControls; 
 
using System.Xml.Linq; 
 

 
namespace WebApplication5 
 
{ 
 
    public partial class xmlCRUD : System.Web.UI.Page 
 
    { 
 
      
 
     protected void Page_Load(object sender, EventArgs e) 
 
     { 
 

 
     } 
 

 
     protected void Button1_Click(object sender, EventArgs e) 
 
     { 
 
      var student = XElement.Load(@"C:\Users\admin\documents\visual studio 2013\Projects\ClassLibrary1\ClassLibrary1\sample1.xml"); 
 
      student.Add(new XElement("Student", 
 
        new XAttribute("ID", TextBox1.Text), 
 
        new XElement("Name", TextBox2.Text), 
 
        new XElement("Gender", TextBox3.Text), 
 
        new XElement("Marks",TextBox4.Text) 
 
       )); 
 
      student.Save(@"C:\Users\admin\documents\visual studio 2013\Projects\ClassLibrary1\ClassLibrary1\sample1.xml"); 
 
     } 
 

 
     protected void Button2_Click(object sender, EventArgs e) 
 
     { 
 
      Button2.Text = "Update"; 
 
      var student = XElement.Load(@"C:\Users\admin\documents\visual studio 2013\Projects\ClassLibrary1\ClassLibrary1\sample1.xml"); 
 
      string val = from m in student.Elements("Student").Where(x=>x.Attribute("ID").Value == TextBox5.Text).Select(x=>x.Element("Student")); 
 
     } 
 
    } 
 
}

<?xml version="1.0" encoding="utf-8"?> 
 
<Students> 
 
    <Student ID="101"> 
 
    <Name>kamal</Name> 
 
    <Gender>Male</Gender> 
 
    <Marks>800</Marks> 
 
    </Student> 
 
    <Student ID="102"> 
 
    <Name>Sapna</Name> 
 
    <Gender>Female</Gender> 
 
    <Marks>900</Marks> 
 
    </Student> 
 
    <Student ID="103"> 
 
    <Name>Raju</Name> 
 
    <Gender>Male</Gender> 
 
    <Marks>870</Marks> 
 
    </Student> 
 
    <Student ID="104"> 
 
    <Name>Sushant</Name> 
 
    <Gender>Male</Gender> 
 
    <Marks>700</Marks> 
 
    </Student> 
 
    
 
</Students>

请帮助我!

回答

0

与此代码在编辑按钮单击事件函数我这样做:

protected void Button2_Click(object sender, EventArgs e) 
 
     { 
 
      Button2.Text = "Update"; 
 
      var student = XElement.Load(@"C:\Users\admin\documents\visual studio 2013\Projects\ClassLibrary1\ClassLibrary1\sample1.xml"); 
 
      var val = (from m in student.Elements("Student") 
 
          where (string)m.Attribute("ID") == TextBox5.Text 
 
          select m); 
 
      //XElement a = (XElement)val; 
 
      if (val.Any()) 
 
      { 
 
       foreach (XElement element in val) 
 
       { 
 
        TextBox2.Text = element.Element("Name").Value; 
 
        TextBox3.Text = element.Element("Gender").Value; 
 
        TextBox4.Text = element.Element("Marks").Value; 
 
       } 
 
      } 
 
      else 
 
      { 
 
       Label1.Visible = true; 
 
      } 
 
     }