Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, I'm coding a program in C# to save some data in to an XML file. I have 3 questions.

1. I'm trying to save my account details in the XML file. The data I want to save is, Account name, Username, Password and Additional info(multiple lines). But I don't know how to arrange the data correctly in to XML elements. I need some help with that.

2. I'm using below code to save data to the XML file. But when I try to save another record, it deletes the existing data from the XML file and then add the new record. So now I have only one record. How do I fix it?

C#
private void button1_Click(object sender, EventArgs e)
        {
            if (Functions.SavetoXML(textBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text) == true) 
            {
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;

                XmlWriter writer = XmlWriter.Create("Passwords.rar", settings);
                writer.WriteStartDocument();
                writer.WriteComment("This file is generated by PasswordSave.");
                writer.WriteStartElement("Account");
                writer.WriteElementString("Name", "" + textBox1.Text);
                writer.WriteElementString("Username", "" + textBox2.Text);
                writer.WriteElementString("Password", "" + textBox3.Text);
               // writer.WriteStartElement("Additinal Info", "" + textBox4.Text);
                writer.WriteEndElement();
                writer.WriteEndDocument();

                writer.Flush();
                writer.Close();
                MessageBox.Show("Information Saved!", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

3. When saving additional info, I have to save multiple lines of information in a XML element how can I achieve that?

Thanks.
Posted
Comments
Johnny J. 5-Jul-13 7:04am    
It's not for me to criticize, but is it really a good idea to write your account details including username and password to a non-encrypted Xml-file?
phantomlakshan 5-Jul-13 7:53am    
I understand. I'm going to do that later though. Would you be able to answer me for the questions??

You could use Write CDATA....
C#
writer.WriteElementString("AdditinalInfo");

// Write CDATA.
writer.WriteCData("write your multi-line text here");
 
Share this answer
 
Comments
phantomlakshan 5-Jul-13 7:54am    
I'll try that. How about arranging my data correctly in XML format?
As per above description it seems u need to append account details in XML file so You can do this in this way as per your work:

using System.Xml.Linq;

private void button1_Click(object sender, EventArgs e)
        {
            string _File="C:/Test.XML";
            if (!File.Exists(_File))
            {
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;
                XmlWriter writer = XmlWriter.Create(_File, settings);
                writer.WriteStartDocument();
                writer.WriteComment("This file is generated by PasswordSave.");
                writer.WriteStartElement("Accounts");
                writer.WriteStartElement("Account");
                writer.WriteElementString("Name", "" + textBox1.Text);
                writer.WriteElementString("Username", "" + textBox2.Text);
                writer.WriteElementString("Password", "" + textBox3.Text);
                writer.WriteEndElement();
                writer.WriteEndElement();
                writer.Close();
              
            }
            else
            {
                Account(_File, textBox1.Text,textBox2.Text,textBox3.Text);
            }
      }

public void Account(string file,string name, string Username, string Password)
        {
           XDocument doc = XDocument.Load(file);
           XElement xeName = new XElement("Account", new XElement("Name", name), new                XElement("Username", Username), new XElement("Password", Password));
           doc.Element("Accounts").Add(xeName);
           doc.Save(file);
        }


I Suggest use XmlDocument or XDocument classes to create XML as well instead of XmlWriter.It will ease your XML working.:)
 
Share this answer
 
v2
Comments
phantomlakshan 7-Jul-13 1:27am    
Thanks. It worked like a charm.

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