如何使用c#在xml文件中存储多个数据 [英] how to store multiple data in xml file using c#

查看:348
本文介绍了如何使用c#在xml文件中存储多个数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI,每个人都希望使用c#在xml中保存多条记录,但是当我尝试插入新记录时,它会覆盖现有记录,我不知道xml,所以PLZ帮助我解决这个问题。



every one i want to save multiple records in xml using c# but when i try to insert new record it's overwrite on existance record i don;t know about xml so plz help me on this topic.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO; 
  

namespace xmlexampleapplication
{
    public class Savexml
    {
        public static void savedata(object obj, string filename)
        {

            XmlSerializer sr = new XmlSerializer(obj.GetType());
            TextWriter writer = new StreamWriter(filename);
            sr.Serialize(writer,obj);
            writer.Close();
        }
    }
}










using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace xmlexampleapplication
{
    public class Information
    {

        private string m_data1;
        private string m_data2;
        private string m_data3;

        public string data1
        {
            get { return m_data1; }
            set { m_data1 = value; }

        }
        public string data2
        {
            get { return m_data2; }
            set { m_data2 = value; }



        }
        public string data3
        {
            get { return m_data3; }
            set { m_data3 = value; }
        }
    }
}










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.Serialization;
using System.IO;


namespace xmlexampleapplication
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                Information info = new Information();
                info.data1 = textBoxdata1.Text;
                info.data2 = textBoxdata2.Text;
                info.data3 = textBoxdata3.Text;

                Savexml.savedata(info,"data.xml");
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}

推荐答案

嗯,是的。它会。

因为你的SaveXml类每次都会创建一个新文件,它会破坏任何现有的数据而不是添加任何内容。

将数据添加到现有的XML文件,你需要读取现有数据,添加新数据,然后保存修改后的版本。
Well, yes. It will.
Since your SaveXml class create a new file each time, that destroys any existing data rather tan adding anything to it.
To add data to an existing XML file, you would need to read the existing data, add the new to it, and then save the modified version.


像你一样使用XmlSerializer将始终只保存一个对象类型的实例。

如果要保存多个信息对象,可以在列表<信息> 上使用XmlSerializer



1)反序列化列表。

2)在列表中追加新记录

3)序列化新列表。



这不是最有效的方式,但它应该是现有代码最接近的方式。



以下是代码示例:

Using the XmlSerializer like you do will always save only one instance of the object type.
If you want to save more than one "Information" object, you can use the XmlSerializer on a List<Information>

1) unserialize the list.
2) append the new record in the list
3) serialize the new list.

It's not the most efficient way, but it should be the closest way of your existing code.

Here is an code sample:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.IO;

namespace XmlSer
{
    public class Information
    {
        private string m_data;
        public string data
        {
            get { return m_data; }
            set { m_data = value; }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Information info = new Information();
                info.data = System.DateTime.Now.ToShortDateString() + " " + System.DateTime.Now.ToShortTimeString();
                AppendData(info, "data.xml");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        static void AppendData(Information obj, string filename)
        {
            XmlSerializer xmlser = new XmlSerializer(typeof(List<Information>));
            List<Information> list = null;
            try
            {
                using (Stream s = File.OpenRead(filename))
                {
                    list = xmlser.Deserialize(s) as List<Information>;
                }
            }
            catch
            {
                list = new List<Information>();
            }
            list.Add(obj);
            using (Stream s = File.OpenWrite(filename))
            {
                xmlser.Serialize(s, list);
            }
        }
    }
}


我用过这个但是情况一样可能我错了这里的任何方式都是代码。





i used this but still situation is same may me i m wrong any way here is code.


public class Information
    {

        private string m_data1;
        private string m_data2;
        private string m_data3;

        public string data1
        {
            get { return m_data1; }
            set { m_data1 = value; }

        }
        public string data2
        {
            get { return m_data2; }
            set { m_data2 = value; }



        }
        public string data3
        {
            get { return m_data3; }
            set { m_data3 = value; }
        }
    }







private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                Information info = new Information();
                info.data1 = textBoxdata1.Text;
                info.data2 = textBoxdata2.Text;
                info.data3 = textBoxdata3.Text;

                Savexml.savedata(info,"data.xml");
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }










public static void savedata(Information obj, string filename)
        {

           
            XmlSerializer xmlser = new XmlSerializer(typeof(List<Information>));
            List<Information> list = null;
            try
            {
                using (Stream s = File.OpenRead(filename))
                {
                    list = xmlser.Deserialize(s) as List<Information>;
                }
            }
            catch
            {
                list = new List<Information>();
            }
            list.Add(obj);
            using (Stream s = File.OpenWrite(filename))
            {
                xmlser.Serialize(s, list);
            }

        }


这篇关于如何使用c#在xml文件中存储多个数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆