With the help of XML your document will be well-ordered and easy to transfer through different applications.
XML is a very popular and useful format of storing data. It stands for Extensible Markup Language, markup language means that apart from the content there are also other elements that determine the look or interpretation of a given content. In such a language you use special tags that begin with < and end with > character. You can for example create <library> object and than within this element <book> objects. With the help of XML your document will be well-ordered and easy to transfer through different applications.
Creating XML file
Now I'll show you how to use XML in your applications in .NET. So open Visual Studio and create new web application. Firstly we will create XML file that we can later use in our application so go to the Solution Explorer window on right and right-click on the name of your program and choose Add - New Item. Then find "XML File" on list in Data tab, name it as you like and click Add button:
In the file that is created we have the following line:
It defines version of XML and character encoding used in the document. As I mentioned before each element of the document is a tag within < and > characters. Unlike in HTML tags are not predefined, you can create them by yourself according to the structure of your data. Each opening tag <...> should have its own closing tag <.../>. Visual Studio is helping you while you create XML document by automatically adding closing tags to each opening tag. Remember that XML document should have always only one root tag. Within this tag you can create as many other tags as you like.
In this tutorial we'll use XML that describes few countries in the world. So add the following code to your XML file:
Here you can see in the picture the schema of this XML document:
Reading XML file
Reading XML in .NET is quite easy because of already existing classes and its methods. In this example we'll use XmlTextReader object and then print its content to the output in Visual Studio. Firstly we have to declare namespaces that we'll use:
Now take a look at the following piece of code:
Firstly we are creating a new XmlTextReader object and specifying the xml file that we want to read. To do this we're using Server.MapPath() method that maps virtual path to physical location on the server. Then according to the type of node we're writing data from xml file to our output. To check the type of node we're using NodeType property, two of the most important values it can have (which we are using in our application) are:
- XmlNodeType.Element - it's simply one element (like for example <country>)
- XmlNodeType.Text - it's a text content of the current node
Element can have children like for instance other elements, it can have also attributes. In our example we iterate through all attributes of each element and print it on the screen.
Writing to XML file
Writing to XML is even easier than reading it. We'll do it using XmlTextWriter class as in the following example:
This code creates an xml file with a list of movies. We used in this example following methods:
- xmlWriter.WriteStartElement - creates a starting tag with specified name
- XmlWriter.WriteEndElement - writes a closing tag for an element
- XmlWriter.WriteElementString - creates a new element with specified name and value
The result of running the code listed above would be the following:
Binding controls to XML
I'll show you also how to bind controls to XML. Firstly let's create such a control on your page, to do this open designer mode of your page in Visual Studio, find GridView control in Toolbox and drag-and-drop it into your page. Now go the the code-behind file of your page - we'll create a data set there, populate it with the content of movies.xml that we have created in previous example and bind it to GridView control. Let's insert the following code in the Page_Load method:
And that's all, after binding movies.xml file to GridView page control should look like this one:
XML is a very useful format to work with data. It makes it easier to read, allows you to separate data from the rest of your application, simplifies transfer of data and is a widely-used standard in web design. What's more as you can see in this tutorial .NET supports extensively XML making it easy to read and write it and allows to bind controls to XML as well.