C#写入xml文件元素访问问题

C#写入xml文件元素访问问题,c#,xml,C#,Xml,我试图将数据从1个数字上下和3个文本框写入xml文件。但是,我无法跨方法访问XDocument文件的元素 下面是我的代码和所有注释。你能帮助一个完整的noob到xml和站点吗?欢迎所有改进建议 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Tex

我试图将数据从1个数字上下和3个文本框写入xml文件。但是,我无法跨方法访问XDocument文件的元素

下面是我的代码和所有注释。你能帮助一个完整的noob到xml和站点吗?欢迎所有改进建议

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.IO;
using System.Xml.Linq;
namespace AA
{
public partial class Form1 : Form
{
    //Will be used to check if the file exists in the save directory
    byte ifexists = 0;

    //method for file creation
    public void fileCreate()
    {
        XDocument XDoc = new XDocument(
            new XDeclaration("1.0", "UTF-16", null),
                new XElement("Group",
                    new XElement("A"),
                    new XElement("B"),
                    new XElement("C"),
                    new XElement("D")
                ));
    }
//method for file save (want to save 4 values from 1 numericUpDown and 3 textboxes, when I solve file creation problems I want to use numericUpDown value as an ID and load relevant info from the xml file
    public void fileSave()
    {
    //i tried to use serverpath which seems to be better but i couldn't get it to work so I used Application.StartupPath....
    XDocument XDoc = XDocument.Load(Application.StartupPath + "\\..\\File.xml");
    Group.Add(new XElement("A", nUDforA.Value));
    Group.Add(new XElement("B", tBforB.Text));
    Group.Add(new XElement("C", tBforC.Text));
    Group.Add(new XElement("D", tBforD.Text));
    XDoc.Add(Group);
    XDoc.Save(Application.StartupPath + "\\..\\File.xml");}

    public Form1()
    {
        InitializeComponent();
    }
    //this label is used to write "Saved!"after clicking the "Write" button and become invisible again after 1 second timertick
    private void Form1_Load(object sender, EventArgs e)
    {
        //this label is used to write "Saved!"after clicking the "Write" button and become invisible again after 1 second timertick
        lblSituation.Visible = false;
    }

    private void btnWrite_Click(object sender, EventArgs e)
    {
    //first check if the file exists   
        if (File.Exists(Application.StartupPath +"\\..\\File.xml"))

{
    ifexists = 1;
    fileSave();
}
        if (ifexists==0)
        {
            fileCreate();
            fileSave();
        }
        //saying the user that the file is saved
        lblSituation.Visible = true;
        lblSituation.Text = "Saved!";
        timer1.Enabled = true;
        }

    private void timer1_Tick(object sender, EventArgs e)
    {
        lblSituation.Visible = false;
        timer1.Enabled = false;

    }
}
}

添加新元素时应使用
XDoc.Root.Add
。什么是
?它是您的第一个元素,但您不能通过其名称访问它,例如:

Group.Add(new XElement("A", nUDforA.Value));
首先使用a、B、C、D元素创建一个新的组元素:

var groupElement = new XElement("Group",
                       new XElement("A", nUDforA.Value),
                       new XElement("B", tBforB.Text),
                       new XElement("C", tBforC.Text),
                       new XElement("D", tBforD.Text));
然后将其添加到xDocument:

XDoc.Root.Add(groupElement);
如果要使用这些值更新第一个组元素,则可以执行以下操作:

var groupElement = XDoc.Descendants("Group").FirstOrDefault();
if(groupElement != null)
{
    var newElement = new XElement("Group",
                       new XElement("A", nUDforA.Value),
                       new XElement("B", tBforB.Text),
                       new XElement("C", tBforC.Text),
                       new XElement("D", tBforD.Text));
    groupElement.ReplaceAll(newElement);
    // save the document
}

注意:您还应该将
XML文档
保存在注释中提到的
fileCreate
方法中。

您的
fileCreate
方法实际上既不保存文件,也不返回
XDocument
。您的
fileSave
方法加载
Dosya.xml
文件,但将其另存为
file.xml
。您的write方法检查是否存在
File.xml
。我已将我的帖子编辑为“Dosya.xml>File.xml”。这是一个翻译错误,对不起@DaveZychThanks对此表示感谢。为了更好地解释我的预期结构,我修改了您建议的第一部分:
var Employees=new-XElement(“Employee”,new-XElement(“ID”,nUD1.Value),new-XElement(“Name”,tBName.Text),new-XElement(“姓氏”,tb姓氏.Text),new-XElement(“Department”,tBDepartment.Text));XDoc.Root.Add(员工)但是,我不想更改第一个条目。我想继续保存信息,例如:1、茱莉亚、曼瑟、会计2、亚当、克拉克、销售等等@塞尔曼22