top of page

C# Xml Easy Read and Write


Xml File
Simple XML File

C# has an extremely powerful XML processor built in. This makes it very easy to serialize and deserialize data in XML format. Visual Studio also has a neat tool for automatically creating the classes based on a schema or even just from the XML file, however I find it much easier to just quickly layout the framework by hand for my projects.



Let's start with defining our data structure. I went with the classic Class/Student type example.


So in our code we will start off like this:


Be sure to add the System.Xml.Serialization namespace. We will define our XMLRoot attribute. This is where the serializer will start taking the data from the XML and matching it up to the class structure. It is important that the letter capitalization and spelling matches what your XML file looks like.

Now looking at our XML structure, we want to have multiple Class elements, so you can see in our code we have a List<Class> with the XmlElement attribute. Now we define the Class class.

If we look back at our XML we notice the the Class element has two Attributes, "name" and "time". In our Class class then we add two strings, with the XmlAttribute attribute ensuring the spelling and capitalization matches the XML. Now, here I have Time just defined as a simple string, however there ways to get more complex data types to serialize other than just string. In this post I just cover basic datatypes.


Each class has a Students element that is populated with a List of students. I could have created a Students class that has a list of Student as a property however, since we know that Students only contains a list of Student, we can short cut it with the [XmlArray("Students"), XmlArrayItem("Student")] attribute.


Now we define our Student.

Like before we have our XmlAttributes defined, and you can see here we have StudentID defined as an int. There is no other special code needed for things like int, double or float.


That defines our XML structure in C# code.


Now for the Reading and Writing. The code to read and write an XML file is always the same, and I find convenient to just make the Read and Write a static template function and just call that with the Class I want to read or write.



And with those two functions, you can read and write any XML file that is defined the way we did it above! XmlSerializer lives in the same System.Xml.Serialization namespace.

Basically get a file stream, create the Serializer based on the class type, and call Serialize or Deserialize!


Now for the test code.


We create our StudentListFile class and the create a new List<Class>. If you are not familiar with the initializer syntax; I'm initializing the List<Class> with two new Class() elements. Then for each class, I'm using the {} syntax to set the properties of the Class to values and with that I'm creating the new List<Student>() for each class. For the students, I'm then doing the same with by using the {} syntax to set the values for each student.


*I think it is important to note that doing the {} syntax to set the values of a new class() property, the values are set after the constructor has been called. So if something in the constructor depends on one of the values being set, it won't be until after it is complete. So if there is value that needs to be set during construction, make sure to pass it in rather than use the {} syntax.*


Then Write out the XML file... Read it back in and print it out to the console!


Console Output
Console Output

And that is it! A very simple way to read and write XML files using C#.




Comments


bottom of page