Reading from an XML File
Reading XML files is a necessary technique to retrieve information from an XML file. Say,…
Extensible Markup Language (XML) allows you to store data in a text and structured format. It is widely used as a database alternative and for storing application configuration information. XML has a similar syntax with HTML so if you know some HTML, then it’s easy to understand XML. XML documents may refer to an XML file in your disk or a string containing XML text. The following is an example of an XML document.
<Persons> <Person> <Name>John Smith</Name> <Age>30</Age> <Gender>Male</Gender> </Person> <Person> <Name>Mike Folly</Name> <Age>25</Age> <Gender>Male</Gender> </Person> <Person> <Name>Lisa Carter</Name> <Age>22</Age> <Gender>Female</Gender> </Person> </Persons>
The XML document is composed of XML elements. An XML element consists of an opening tag, a closing tag, and the data or child elements between the opening and closing tags. You can provide any name for the elements as long as its describes the data that it will hold. Please note that elements are case-sensitive so persons and Persons are two different things. Also, note that XML ignores white spaces so you can write an XML file in one line although formatting XML properly makes it easier to read.
Elements may contain child elements which may also contain another child element.
<parent> <child1>data</child1> <child2> <grandchild1>data</grandchild1> </child2> </parent>
The XML document shown in Figure 1 contains data for three persons. Every XML document must have a root element which serves as the top level element. In Figure 1, the element named Persons is the root element and every element inside it are its child elements. Each person’s details is enclosed inside a Person element. Inside a Person element, you can find child elements for the name, age, and gender of a person.
XML Attributes which is another way of adding a data to an element. For example, the following is an element with an XML attribute inside it.
<Person name="John Smith">some data</Person>
The element above has one attribute named “name” and a value of “John Smith”. Values must be enclosed in quotation marks(“) or single quotation marks(‘). The following is the syntax of adding attributes.
<element att1="value1" att2="value2" ... attN="valueN">data</element>
As you can see, we can add multiple attributes to an element.
<Person name="John Smith" age="30" gender="Male">some data</Person>
Let’s update our XML document in Figure 1 to add attributes to each element.
<Persons> <Person name="John Smith"> <Age>30</Age> <Gender>Male</Gender> </Person> <Person name="Mike Folly"> <Age>25</Age> <Gender>Male</Gender> </Person> <Person name="Lisa Carter"> <Age>22</Age> <Gender>Female</Gender> </Person> </Persons>
We removed the Name element for each person and instead, we provided an equivalent attribute for each element.
XML documents can also have an XML declaration. The XML declaration contains information about the XML document such as the version of XML used (1.0 is always recommended) and the text encoding used.
<?xml version="1.0" encoding="UTF-8" ?>
The XML declaration is placed in the top-most part of the document just before the root element.
You can also write comments in an XML file. The following is an example of an XML comment.
<!-- This is an XML comment -->
We can create an XML file using a text editor, but Visual Studio has a great XML editor that automatically formats and uses IntelliSense as you type. It will also help you prevent badly structured XML as Visual Studio will highlight them with red or green squiggly lines. When you create a new project, you can click the Add New Item button in the toolbar and when the Window shows up, choose XML file. Visual Studio will automatically create an XML file with an XML declaration.