Click here to Skip to main content
15,884,425 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi, everyone I want to store user's multiple data in xml using c# but when user gives Multiple data only the last data is stored to the xml file. It's overwritten on existance record. What can I do to store multiple user's data ??
So help me guys to solve this problem.

What I have tried:

using System;
using System.Xml;
namespace ReadingXML2
{
    class Class1
    {
        static void Main(string[] args)
        {
            string name, name1;
            int num, i;
            Console.Write("Number Of Student Details = ");
            num = Convert.ToInt16(Console.ReadLine());
            for (i=0; i < num; i++)
            {
                Console.Write("Name = ");
                name = Console.ReadLine();
                Console.Write("College Name : ");
                name1 = Console.ReadLine();

                XmlTextWriter writer = new XmlTextWriter("C:\\Documents\\File\\sample.xml", null);

                writer.WriteStartDocument();
                writer.WriteStartElement("log");
                writer.WriteStartElement("data");
                writer.WriteStartElement("Name");
                writer.WriteString("" + name);
                writer.WriteEndElement();
                writer.WriteStartElement("CollegeName");
                writer.WriteString("" + name1);
                writer.WriteEndDocument();
                writer.Close();
                Console.ReadLine();
            }
        }
    }
}
Posted
Updated 16-May-17 23:51pm
v6
Comments
RickZeeland 17-May-17 10:21am    
I see you marked your own solution as the answer, but although the BindingList method might seem too complex for you now, I would still recommend at least taking a look at it. The time invested in this technique can be of great benefit to you in a later stadium !

It would probably be easier to use a BindingList and serialize to XML.
Here is an excellent example: A Detailed Data Binding Tutorial[^]
If you want information about serializing to XML, see: [^]

Example:
using System.Xml.Serialization;

	public static void TestSerialization()
	{
		myClass person = new myClass();
		List<myClass> listStr = new List<myClass>();

		listStr.Add(person);
		// listStr.Add(new myClass() { Check = true, Group = "GroupTwo" });

		var x = new XmlSerializer(listStr.GetType());

		using (var strWriter = new StringWriter())
		{
			x.Serialize(strWriter, listStr);
			File.WriteAllText("test.xml", strWriter.ToString());
		}
	}
 
Share this answer
 
v2
using System;
using System.Xml;
namespace ReadingXML2
{
    class Class1
    {
        static void Main(string[] args)
        {
            string name, name1;
            int num, i;
            Console.Write("Number Of Student Details = ");
            num = Convert.ToInt16(Console.ReadLine());
            using (XmlTextWriter writer = new XmlTextWriter("C:\\Documents\\File\\file.xml", null))
            {
                writer.WriteStartDocument();
                writer.WriteStartElement("log");
                for (i = 0; i < num; i++)
                {
                    Console.Write("Name = ");
                    name = Console.ReadLine();
                    Console.Write("College Name : ");
                    name1 = Console.ReadLine();

                    writer.WriteStartElement("data");
                    writer.WriteStartElement("Name");
                    writer.WriteString(name);
                    writer.WriteEndElement();
                    writer.WriteStartElement("CollegeName");
                    writer.WriteString(name1);
                    writer.WriteEndElement();
                    writer.WriteEndElement();

                }
                writer.WriteEndElement();
                writer.WriteEndDocument();
                writer.Close();
            }
                       
        }
    }
}
 
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