单击“保存”时,Xml添加新行 [英] Xml Add New Line When I Click Save

查看:48
本文介绍了单击“保存”时,Xml添加新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

保持简短和甜蜜,我想每次点击按钮时都为我的XML添加新行...我刚开始学习:)



我有2个CS文件;



第一个XML.cs



Keeping it short and sweet, I want to add new lines to my XML every time I click the button... I'm just starting to learn :)

I have 2 CS Files;

First XML.cs

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


namespace No_IdeaV2.API_Key_Window
{

    public class XML
    {
        static void main (string[] args)
        {
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load("data.XML");
            XmlNodeList userNodes = xmldoc.SelectNodes("data.XML");
            foreach(XmlNode userNode in userNodes)
            {
                
            }
        }
        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();
        }

    }
}





第二个APIKEYS.cs





the second APIKEYS.cs

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

namespace No_IdeaV2.API_Key_Window
{
    public class APIKEYS
    {

        private string APIkey;
        private string VCode;

        public string APIKEY
        {
            get { return APIkey; }
            set { APIkey = value; }
        }
        public string VCODE
        {
            get { return VCode; }
            set { VCode = value; }

        }
    }
}





我的按钮是;





My Button is;

private void button1_Click(object sender, EventArgs e)
        {
            try
            {
  
               APIKEYS info = new APIKEYS();
            info.APIKEY = txtAPI.Text;
            info.VCODE = txtVerC.Text;
                XML.SaveData(info, "data.XML");
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }





最后我的XML输出看起来像这样;





Lastly my XML out put looks like this;

<?xml version="1.0" encoding="utf-8"?>
<APIKEYS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <APIKEY>1234567</APIKEY>
  <VCODE>132456789</VCODE>
</APIKEYS>





我想要做的就是每当我点击保存它就会添加新数据而不是覆盖它。



感谢您的帮助!!



What I want to do is whenever I click Save it'll add in the new data instead of overwriting it.

Thanks for the help!!

推荐答案

追加的方式取决于你的要求。您可以简单地定义StreamWriter以便附加,例如

The way to append depends on your requirements. You could simply define the StreamWriter for append like
TextWriter writer = new StreamWriter(Filename, true);



但我怀疑这不是你所追求的,因为那也将重复开头的标签(xml和命名空间)。

为了向文件添加多个对象,一种方法是使用通用列表并将所需的对象添加到其中。之后序列化列表。类似




But I doubt that's not what you're after since that will also repeat the opening tags (xml and namespace).
In order to add more than one object to the file, one way would be to use a generic list and to add the desired objects into it. After that serialize the list. Something like

public System.Collections.Generic.List<apikeys> apikeyslist = new System.Collections.Geneiric.List<apikeys>();</apikeys></apikeys>



...


...

APIKEYS info = new APIKEYS();
info.APIKEY = txtAPI.Text;
info.VCODE = txtVerC.Text;
apikeyslist.add(info);
XML.SaveData(apikeyslist, "data.XML");


如果你想使用 XmlDocument ,你首先解析它,使用API​​在内存中添加节点,并且然后再将所有内容存储在文件中。



-SA
If you want to work with XmlDocument, you first parse it all, add the node in memory using the API, and then store everything in a file again.

—SA


这篇关于单击“保存”时,Xml添加新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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