-3

ここで見つけたものとは異なり、xml ドキュメント内で構文を維持したいのですが、シリアライゼーションはそれに触れていません。XMLドキュメントに別の「タスク」タグを追加できるようにしたい...情報のロードは問題ではありません。以前はそれに対処しなければなりませんでした...しかし、これはそうです。

主なプログラム:

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>
4

1 に答える 1

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>

数日前、同様の方法でリストをシリアライズすることに関する別の質問に答えました。彼の質問と答えをチェックしてください。それはあなたがやろうとしていることかもしれません.

于 2012-06-22T20:44:37.757 に答える