【问题标题】:Saving Xml to a Document C#将 Xml 保存到文档 C#
【发布时间】:2012-06-22 20:33:51
【问题描述】:

与我在这里找到的不同,我想在我的 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>

【问题讨论】:

标签: c# xml windows save


【解决方案1】:

你可以用来序列化的类:

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

XmlElementAttribute 放在集合类型上会导致每个元素都被序列化,而不是为列表放置一个节点。

带有XmlElementAttribute的Xml:

<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关于以类似方式序列化列表的问题。看看他的问题,然后是答案,这可能就是你想要做的。

【讨论】:

  • 我如何使用这个类或什么?比如, public List Tasks { get; 中的“任务”是什么?放; }需要做...我真的很困惑
猜你喜欢
  • 2020-03-26
  • 1970-01-01
  • 2018-07-22
  • 2013-11-11
  • 1970-01-01
  • 2017-09-09
  • 1970-01-01
  • 2014-12-16
  • 2017-08-20
相关资源
最近更新 更多