by kirupa |
18 January 2007XML, or Extensible
Markup Language, is a very popular format used to store and
share data. In a nutshell, XML stores information in a
tree-based text format that allows both you and I as well as
computers to easily read the data. I'm sure you have used XML-like
languages directly or indirectly, to borrow two popular
examples, if you have ever used RSS feeds or have written
XHTML pages.
In this tutorial, I will explain how to read
data from an XML file in C#. The .NET Framework provides
built-in functionality for reading and writing XML, but
knowing how to use those classes can be important. Before
diving into the code, I want to provide a brief overview of
XML and cover some terminology because it will help you to
better understand why the code does something of the things
it does.
The following image shows how a simple XML
file looks like:
There are some basic things to notice about
the structure of the XML file. The text menu,
parent, child,
title, and link are known as
elements, and each element consists of a start tag <foo> and
a end tag </foo>. Notice that the elements are designated
with brackets < and >, and the end tag also starts off with the
backslash / character.
Elements can be nested, and the nested
content can either be attributes or other elements. With
that said, let's revisit the above image and look at two
examples of nested content. First, let's look at an example
of nested elements:
The title and
link elements are nested inside the child
element, and the child element itself is nested under the
parent and menu elements.
How is that for nesting?
Our example file also contains an
example of another type of nested content - attributes. Take a look at the the parent
element:
Notice that the parent element contains the
title attribute title with the value
kirupaPicks. There are really the two big XML details that
you need to know. In the next page, I will present some C#
code and explain how it works to help your computer to read XML data.
Onwards to the
next page!
|