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

Dani AI

Generated

A quick, practical summary that complements the replies from , and and shows a safe, maintainable way to handle XML in an ASP.NET/C# app.

For choice of approach: tabular XML works well with DataSet-style APIs, serializing/deserializing objects works when you have fixed types, and LINQ to XML (XDocument/XElement) is the most flexible for reading, querying and editing document-shaped XML. For very large files prefer streaming (XmlReader/XmlWriter) to avoid loading the whole file.

A simple pattern using LINQ to XML (classic ASP.NET example that reads, projects and appends a node):

using System.Xml.Linq;
using System.Web.Hosting;
using System.Linq;

string filePath = HostingEnvironment.MapPath("~/App_Data/people.xml");

var doc = XDocument.Load(filePath);
var people = doc.Root.Elements("person")
    .Select(x => new {
        Name = (string)x.Element("name"),
        Age = (int?)x.Element("age") ?? 0
    }).ToList();

doc.Root.Add(new XElement("person",
    new XElement("name", "New Person"),
    new XElement("age", 42)
));
doc.Save(filePath);

Safe-write pattern (avoid partial writes and simple concurrency issues):

string temp = filePath + ".tmp";
using (var fs = new FileStream(temp, FileMode.Create, FileAccess.Write, FileShare.None))
{
    doc.Save(fs);
}
File.Replace(temp, filePath, null); // atomic replace on the same volume

Notes and troubleshooting: always dispose streams (using), catch XmlException/IOException, validate with an XSD if structure matters, ensure the App_Data folder has proper write permissions, and avoid concurrent multi-user edits to flat XML — use a database or a coordinated update strategy for that. If you prefer typed round-trips, serialization is fine but requires public, serializable types and a parameterless constructor.

Recommended Answers

All 2 Replies

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.