Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: XML Visual-Studio C#4.0
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?
 
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 5-Jul-13 1:48am
Comments
Johnny J. at 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 at 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??
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You could use Write CDATA....
writer.WriteElementString("AdditinalInfo");
 
// Write CDATA.
writer.WriteCData("write your multi-line text here");
  Permalink  
Comments
phantomlakshan at 5-Jul-13 7:54am
   
I'll try that. How about arranging my data correctly in XML format?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.Smile | :)
  Permalink  
v2
Comments
phantomlakshan at 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)

  Print Answers RSS
0 Mathew Soji 334
1 Maciej Los 295
2 BillWoodruff 290
3 Afzaal Ahmad Zeeshan 257
4 DamithSL 225
0 OriginalGriff 6,369
1 Sergey Alexandrovich Kryukov 5,973
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 4,045


Advertise | Privacy | Mobile
Web04 | 2.8.1411019.1 | Last Updated 5 Jul 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100