我想在c#中更新xml元素的'属性和它的嵌套元素值

本文关键字:元素 属性 嵌套 更新 xml | 更新日期: 2023-09-27 18:15:22

基本上我已经创建了一个webform,让用户添加,编辑和删除元素(或记录)的现有xml文件,让用户写入他/她的数据值到文本框。现在问题来了,当我已经创建了一个按钮来编辑/更新元素的值。每当单击此按钮时,用户将被要求输入已经存在的Student元素的属性值ID,然后将在我的linq查询代码中检查该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>
    
        &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>

请帮帮我!

我想在c#中更新xml元素的'属性和它的嵌套元素值

在edit button click event函数中使用以下代码:

   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;
            }
        }