Sometimes,
when we are creating small application, we have to use SQL Server database if
we don’t know about any alternative solution. SQL Server databases are very
familiar with programmers, but if our application is small then we don’t let
the user install and maintain any database engine.
We
have an alternative solution i.e. XML (Extensible Markup Language) files that
is a textual data format. It is widely used for the representation of arbitrary
data structures, for example in web services. We can read more about Xml files
through http://en.wikipedia.org/wiki/XML.
To
work with XML files we use XmlSerializer class that serializes and deserializes
objects into and from XML documents. It is in System.Xml.Serialization namespace and enables us to control how objects are encoded
into XML. The most important methods in this class are Serialize and
Deserialize.
In this article we will
serialize a class Person consists of public properties, as shown below:
public class Person
{
public string Name { get; set; }
public string FName { get; set; }
public int Age { get; set; }
}
Write data to XML file:
To write data in XML file we
use Serialize method that serializes the specified object and
writes the XML document to a file using the specified stream.
It takes two parameters.
First parameter will be used to write the data to file, it
may be either a FileStream or an XmlWriter. Second parameter is the data that is to be written.
List<Person> personList = new List<Person>();
XmlSerializer serializer = new XmlSerializer(typeof(List<Person>));
using (FileStream stream = new FileStream(Environment.CurrentDirectory + "\\file.xml", FileMode.Create, FileAccess.Write))
{
serializer.Serialize(stream, personList);
}
In above code
- Add some data to personList that are to be written to file.personList.Add(new Person() { Name = "Student1", FName = "Father1", Age = 25 });personList.Add(new Person() { Name = "Student2", FName = "Father2", Age = 24 });personList.Add(new Person() { Name = "Student3", FName = "Father3", Age = 26 });personList.Add(new Person() { Name = "Student4", FName = "Father4", Age = 23 });
- Create an object of XmlSerializer class which will decide the type of data that is to be written (list of person here).
- Create a stream having FileMode Create and FileAccess Write.
- At the last serialize the list to stream and stream will write it to xml file.
Read data from XML file:
To read data from XML file we use Deserialize method
that deserialize the xml document contained by stream. It takes only one
parameter i.e. our stream.
List<Person> accessedData = new List<Person>
XmlSerializer serializer = new XmlSerializer(typeof(List<Person>));
using (FileStream stream = new FileStream(Environment.CurrentDirectory + "\\file.xml", FileMode.Open, FileAccess.Read))
{
accessedData = serializer.Deserialize(stream) as List<Person>;
}
dataGridView1.DataSource = accessedData;
In above code
- Declare a list of person that will hold deserialized data.
- Create an object of XmlSerializer class which will decide the type of data that is to be read (list of persons here).
- Create a stream having FileMode Open and FileAccess Read.
- At the last deserialize the data and convert them to List<Person>.
After successfully deserialize the data in list, i am bind that list to datagridview. When i run the project, my datagridview look like following screenshot:
We can read more about Environment.CurrentDirectory