基本上我已经创建了一个webform,允许用户通过让用户将他/她的数据值写入textbox来添加,编辑和删除现有xml文件的元素(或记录)。当我创建时,问题出现在这里用于编辑/更新元素值的按钮。只要单击此按钮,用户将需要输入已存在的Student元素的ID属性值,然后将在我的linq查询代码中检查该ID,就像该ID一样是否存在,如果存在,则将所有其他嵌套元素值打印到所有文本框中。 下面是我的整个代码和xml文件:
<%@ 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>
<br />
Student-ID :
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<br />
Student Name:
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
<br />
Gender :<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<br />
<br />
Total Marks :
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Add Student" />
<br />
<br />
<asp:Button ID="Button2" runat="server" Text="Edit Data" OnClick="Button2_Click" />
<asp:TextBox ID="TextBox5" runat="server" Placeholder="Enter student-ID" Visible="False"></asp:TextBox>
<br />
<br />
<br />
<br />
<br />
</div>
</form>
</body>
</html>
&#13;
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"));
}
}
}
&#13;
<?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>
&#13;
请帮助我!
答案 0 :(得分: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;
}
}
&#13;