2014-01-27 88 views
0

我試圖從1個numericUpDown和3個文本框中寫入數據到一個xml文件。但是,我無法跨方法訪問XDocument文件的元素。C#寫入xml文件元素訪問問題

下面是我的代碼和我所有的評論。你能幫助一個完整的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; 

    } 
} 
} 
+0

你的'fileCreate'方法實際上並不保存文件,也不返回'XDocument'。你的'fileSave'方法加載一個'Dosya.xml'文件,但將其保存爲'File.xml'。你的寫方法檢查是否存在'File.xml'。 –

+0

我已將我的帖子編輯爲「Dosya.xml> File.xml」。這是一個翻譯錯誤,對不起。 @DaveZych – user3242180

回答

0

你應該使用XDoc.Root.Add當你添加新的elements.What是Group?這是你的第一要素,但是,你不能用它的名字一樣,訪問它:

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 Document在您的fileCreate方法,如評論中所述。

+0

感謝您的評論。爲了更好地解釋我的預期結構,我將您的建議的第一部分修改爲:var Employees = new XElement(「Employee」, new XElement(「ID」,nUD1.Value), new XElement(「Name」,tBName .Text), new XElement(「Surname」,tBSurname.Text), new XElement(「Department」,tBDepartment.Text)); XDoc.Root.Add(Employees);' 但是,我不想更改1st條目。我想繼續保存信息,如:1,朱莉婭,曼瑟,會計 - 2,亞當,克拉克,銷售 - 等等.. @ selman22 – user3242180