2012-06-22 87 views
-3

與我在這裏可以找到的不同,我用魔杖來維護XML文檔中的語法,並且序列化沒有涉及到這一點。我希望能夠向xml文檔添加另一個「任務」標籤......加載信息不是問題,我必須先處理這個問題......但是這是。將Xml保存到文檔C#

主要課程:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Xml; 
using System.Xml.Linq; 
using System.Xml.Serialization; 
using System.IO; 

namespace ToDoList 
{ 
    public partial class Form1 : Form 
    { 
     string title; //the variable for the title textbox value to be stored in 
     string details; //the variable for the details textbox value to be stored in 
     string itemstr; //the variable for title and details to be merged in 

     public Form1() 
     { 
      InitializeComponent(); 
     } 

     public void Form1_Load(object sender, EventArgs e) 
     { 
      optionsbtn.Text = "Options"; //make the options button's text options 
      var items = ToDochkbox.Items; //create a private "var" items symbolizing    the Checkbox's items array 
      XDocument xmlDoc = XDocument.Load("tasksdoc.xml"); //load the xml document   (in bin or release) 
      var q = from c in xmlDoc.Descendants("root") //go "within" the <root>  </root> tag in the file 
        select (string)c.Element("task"); //find the first <task></task> tag 
      foreach (string N in q) //now cycle through all the <task></task> tags and  per cycle save them to string "N" 
      { 
       items.Add(N); //add the item to the checkbox list 
      } 


     } 

     public void addbtn_Click(object sender, EventArgs e) 
     { 
      var items = ToDochkbox.Items; //create a private "var" items symbolizing  the Checkbox's items array 
      title = Addtb.Text; //set the title string to equal the title textbox's  contents 
      details = detailstb.Text; //set the details string to equal the detail  textbox's contents 
      itemstr = title +" - " + details; //set a variable to equal the title string, a - with spaces on each end, and then the details string 
      items.Add(itemstr); //add the variable itemstr (above) to the the checkbox list 

     } 

    private void optionsbtn_Click(object sender, EventArgs e) 
    { 
     new options().Show();//show the options form 
    } 
    private void aboutToolStripMenuItem_Click(object sender, EventArgs e) 
    { 
     new options().Show();//show the options form 
    } 

    private void saveToolStripMenuItem_Click(object sender, EventArgs e) 
    { 

    } 

    public void loadToolStripMenuItem_Click(object sender, EventArgs e) 
    { 
     optionsbtn.Text = "Options"; //make the options button's text options 
     var items = ToDochkbox.Items; //create a private "var" items symbolizing the Checkbox's items array 
     XDocument xmlDoc = XDocument.Load("tasksdoc.xml"); //load the xml document (in bin or release) 
     var q = from c in xmlDoc.Descendants("root") //go "within" the <root> </root> tag in the file 
       select (string)c.Element("task"); //find the first <task></task> tag 
     foreach (string N in q) //now cycle through all the <task></task> tags and per       cycle save them to string "N" 
      { 
       items.Add(N); //add the item to the checkbox list 
      } 


     } 


    } 
} 

和我的XML文檔:

<root> 
     <task>First Task - Create a Task</task> 
    </root> 
+1

那麼問題是什麼? –

+1

什麼是「在我的xml文檔中維護語法的魔杖」是什麼意思,以及爲什麼你認爲「序列化不會觸及那個」? –

+1

請參閱「[堆棧溢出不允許在標題中使用標記](http://meta.stackexchange.com/a/130208)」 –

回答

0

,你可以使用序列化類:

public class MyClass 
{ 
    [XmlElement("task")] 
    public List<string> Tasks { get; set; } 
} 

上集合類型放置XmlElementAttribute會導致每個元素被序列化而不被放置到列表的節點。

的Xml與XmlElementAttribute

<root> 
    <task>First Task - Create a Task</task> 
    <task>SecondTask - Create a Task</task> 
    <task>ThirdTask - Create a Task</task> 
</root> 

的Xml 沒有XmlElementAttribute

<root> 
    <Tasks> 
     <Task>First Task - Create a Task</Task> 
     <Task>SecondTask - Create a Task</Task> 
     <Task>ThirdTask - Create a Task</Task> 
    </Tasks> 
</root> 

我回答another question約幾天前以類似的方式序列化列表。看看他的問題,然後回答,這可能是你想要做的。

+0

我該如何使用這個類或什麼?比如,公共列表中的「任務」列表任務{get;組; }需要做...我真的很困惑 – SubConcussive