XML
XML is one of the data format which programmers like to use it storing data. There are 3 different ways to output xml file in C#. In this article, I use a simple window form program to demonstrate how to output xml format in C#
Before, we move to the implementation. I show what is xml firstly.
<Im>
<Number>xx</Number>
<Name> xxx</Name>
<Type> xxx <Type>
<Data> xxx </Data>
</Im>
The above text I write in XML format. XML format is composed by node and each node uses to store something you like. The following picture is a car picture, I use it as an example in this article to show you how to store it and its meta data in xml format.
The final output of xml show as the follow
And the store process is done by my own window form program.
XML output ways
1.XMLElement
Elements are the most common things in xml model, Each element has its attribute and may have its children. You can create each element independently and append them to parent finally. The top element in xml structure we call it "root". The following code demonstrate how to create a xml file using XMLElement in c#. Firstly, you create a XmlDocument as XML file. Secondly, you create all sub nodes step by steps. Finally, just like I mention before, you can place all sub nodes to root node lastly and use the "save" function to output a xml file. However, for the situation of storing binary data, you should convert the binary data to base64 string firstly and then store to XmlElement.
2. XmlTextWriter
XmlTestWriter is default class provided by Microsoft, it is fast, non-cache and forward only to streaming the xml format data.
Compared with “XMLElement way”, the sequence of data is very important. You have to ensure the sequence of your input. Because the process of manipulating xml data is streaming. It's mean that you are not place the store data in right xml format sequence. The final output will be disrupt.
3. Serialization
I personally prefer this way to manipulate xml data in C#. Because it treats the xml data as object. If you want to convert xml data, you just invoke "serialize" function. On the other hand., you can convert the xml data to object easily by invoke "deserialize" function. However, before you use this method, you need to define a class and map its' properties map to xml elements. Those steps may be tedious but useful for future development especially you are creating a library project. In order to make this class reusable. I define a function called "toXMLDocument”. It can convert this object to XmlDocument type.
In the above example, I create name, type, number and data inside ImgXml class. The "XmlElementAttribute" is a serialization tag in C#. It means that those attributes will be consider as an XmlElement in "serialization" process. Inside "toXmlDocument" function. I create a "XmlSerializer" object to manipulate the process of convert ImgXml to xml data and "XmlSerializerNamespaces" object to manipulate the header of xml file.
Due to I encapsulate the converting logic inside "toXmlDocument" method. Therefore, I can convert the ImgXml object to XML simply by invoking this method in window form program.
Wrap-up
In this article, I introduce three different ways to generate xml document. They are “XMLElement”, “XmlTestWriter” and "Serialization". I personally suggest using "Serialization" way to generate xml because the code style is cosine, reusable and match the OOP design principle. I put the above source code in my github
I hope you enjoy this article.
Please leave your email below.