Click here to Skip to main content
15,898,010 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
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();
        }

    }
}


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);
            }
        }


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!!
Posted
Comments
Sergey Alexandrovich Kryukov 21-Aug-15 11:57am    
Add or insert? If you append any line to XML (except, perhaps a comment), the result won't be XML...
Anyway, XML is not made of lines...
—SA
Member 11841936 21-Aug-15 12:23pm    
I want to insert a new node?(I think) so on my example we have <apikey>1234567
if I enter in new data i'd like to add APIKEY1... APIKEY2... for new data same with the VCODE.. VCODE1..2

as I I'm going to need to use that information later =/
Sergey Alexandrovich Kryukov 21-Aug-15 15:22pm    
All right, but what's the problem. 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
Member 11841936 24-Aug-15 3:04am    
Do you have an example what this might look like?
Sergey Alexandrovich Kryukov 24-Aug-15 8:48am    
No, why? Just try it.
—SA

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

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

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

...
C#
APIKEYS info = new APIKEYS();
info.APIKEY = txtAPI.Text;
info.VCODE = txtVerC.Text;
apikeyslist.add(info);
XML.SaveData(apikeyslist, "data.XML");
 
Share this answer
 
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900