hi,
i have a xml file and i would like to read and write data in XML file.
thanks
regards
umesh chand daiya

Here is an example of code I use to read an XML file.

void binddata()
    {
        DataSet dsName = new DataSet();
        dsName.ReadXml("C:/XMLFILEHERE"));
        foreach (DataColumn da in dsName.Tables[0].Columns)
        {
            da.ColumnMapping = MappingType.Attribute;
        }
     }

Please have a look at the code below :

public class Person
    {
        public string Name= "";
        public int age;       
        public void accept()
        {
            Name = Console.ReadLine();
            age= Int16.Parse(Console.ReadLine());
        }
    }
    class Program
    {
        public static void Main()
        {
		//Serialisation--------------
                FileStream fs = new FileStream(@"d:\newfile.xml",FileMode.OpenOrCreate,FileAccess.ReadWrite);
            
                Person obj = new Person();
                obj.accept();

                XmlSerializer ser = new XmlSerializer(obj.GetType());
                StreamWriter wr = new StreamWriter(fs);
                ser.Serialize(wr, obj);
		fs.Close();

		//De-Serialisation--------------
                XmlDocument doc = new XmlDocument();
                doc.Load(@"d:\newfile.xml");
                XmlNodeReader reader = new XmlNodeReader(doc.DocumentElement);
                XmlSerializer ser1 = new XmlSerializer(obj.GetType());

                object obj1 = ser1.Deserialize(reader);
                Person myObj = (Person)obj1;
                Console.WriteLine(myObj.Name);

        }
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.