To differentiate the various nodes that belong to an XML
file, they are classified by their category. The types of
node are listed in the XmlNodeType enumeration.

Creating a Comment

A comment is a character, a line or a paragraph that is not
considered as part of the XML code that needs to be processed. A comment allows
you to insert notes or personal observations inside an XML file. For this reason,
a commented section can be written any way you like. This means that a comment
can include plain text, formulas, expressions, or even XML code as long as you
know that that XML code will not be validated: it will be ignored by the parser.

To manually create a comment, you use the following formula:

<!-- Blah Blah Blah ->

Between <!-- and -->, any text in that section is
considered a comment and you can include anything you want. Both sections
of the comment use two dashes, not more, not less. Here is an example:

To support comments, the System.Xml provides a class
named XmlComment. To allow you to programmatically create a comment, the
XmlDocument class is equipped with a method named CreateComment. Its signature is:

Public Overridable Function CreateComment(ByVal data As String) As XmlComment

This method takes as argument the text that would go into
the commented section. After calling it, if the method succeeds, which it
usually does, it returns the XmlComment object that was created. Here is
an example of creating a comment:

Like any other part of an XML file, a comment is represented
by the XmlComment.Name
property. This allows you to retrieve the name of a comment that is included in
the document.

CDATA

Introduction

Except for comments, the parser is used to "scan"
the whole XML file to analyze it. Every tag is then interpreted. As we mentioned
already, the value of each tag can be displayed in a browser between its opening
and its closing tag, and the browser uses different font styles to make a
distinction. When creating some tags and some sections of the file, you may want
the parser to consider those particular tags and sections as regular text. That
is, you may want the parser to treat a certain tag and its value as if it were
regular text even though it is created as an XML file.

To prevent the parser from interpreting a tag regularly but
to treat that tag and its value as regular text, you can create it in a CDATA
section.

Creating a CDATA Section

To do this, create a section that starts with <![CDATA[,
followed by anything you want, and ending with ]]>. The formula used
is:

<![CDATA[ Blah Blah Blah ]]>

Between <![CDATA[ and ]]>, you can
type anything, including one or more normal XML tags. Here is an example:

To support CDATA
section, the .NET Framework provides a class named XmlCDataSection. To
allow you to programmatically create a CDATA section, the XmlDocument class
is equipped with the CreateCDataSection() method. Its signature is:

Public Overridable Function CreateCDataSection(ByVal data As String) As XmlCDataSection

This class is equipped with a
Name property that allows you t retrieve the name of a CDATA section in
an XmlDocument object.

XML Namespaces

Introduction

The Internet is filled with XML documents.
Even inside of a company or a department, many people create XML or XML-types of
documents on a daily basis. One of the things that these many documents share is
that they use same words or names, by coincidence or without any intention. The
ultimate problem is that, when two XML documents are accessed in the same
application, if those two documents use the same name, there could be a conflict
of names.

A namespace is a technique of qualifying a name so that it
would not conflict with another name. Normally, you should already be familiar
with namespaces because they are used in F#. The concept is primarily the same in XML. You create a
namespace in your XML document so that its elements and attributes can be
accessed outside that document without worrying about name conflicts because the
names of your document will be qualified.

The Construction of a Namespace

In XML, a namespace is made of two parts: a namespace name
and a local name.

As you may know already, to open an XML document, you can
use a browser. For example, if you have a document named employees.xml and that
is hosted on the web site of funfunfun.com, you can open it using
http:'www.funfunfun.com/employees.xml. Another web site, such as
blahblahblah.com, can have a file named employees.xml and that file can be
accessed with http:'www.blahblahblah.com/employees.xml.

Creating a Namespace

A namespace is created as an XML attribute. This means
that you must create an element that has a name. Here is an example:

<business></business>

The keyword to create an XML namespace is xmlns. The formula to create a namespace is:

PrefixAttributeName | DefaultAttributeNameName

This means that you primarily have two options:

You can create a namespace to which you will specify something called a
prefix. A prefix is a name by which the elements of the document will be
accessed. In this case, you type xmlns followed by a colon ":" and a prefix,
which can be any valid name. Here is an example:

<business xmlns:CarRental></business>

You can create a namespace without a prefix. In this case, you simply or
only use xmlns

In an XML namespace, binding consists of associating an element
to a prefix. To create this binding, you must assign a value to your namespace. The value
must be a Uniform Resource Identifier (URI), which is an address of a document
that resides somewhere, on the World Wide Web or in a computer. When it comes to
XML, the URI must identify a namespace.

There are two types of URI documents:

Uniform Resource Locator (URL): A URL defines an address of a document
on the Internet. Its value starts with the http protocol
followed by :' to indicate that an address follows. Here are examples:

Uniform Resource Name (URN): A URN is another type of URI address that
uses a different. It starts with a URN or urn prefix and a colon
":". This time, the forward slashes "'" are not used. Here are examples:

Any of them can be used to identify a namespace. Remember that, when creating a namespace, you have two options:

If you decide to use a prefix, assign the URL to xmlns:Prefix. Here is an example:

<business xmlns:CarRental="http:'www.abcd1234.com/rent"></business>

If you use the second option that omits a prefix, simply assign the URL
to xmlns. Here is an example:

<business xmlns="http:'www.abcd1234.com/"></business>

In this case, the URL is referred to as the default namespace

This whole formula is referred to as a namespace
binding. In this formula, xmlns and what follows it is not considered an
attribute in the traditional sense. This means that if you try to access the
collection of attributes of the tag, xmlns and its expression will not be
considered as a member of that collection.

In addition to these, there are a few rules you must
follows:

If you decide to use a prefix:

Don't use xml as the name of the prefix because it is
bound to the http:'www.w3.org/XML/1998/namespace URL. That is, don't create
something like:

<business xmlns:xml="http:'www.abcd1234.com/rent"></business>

Don't start the name of a prefix with the letters X, M, and L in any
case (lowercase or uppercase)

Don't use xmlns as the name of the prefix. It is bound to http:'www.w3.org/2000/xmlns. That is, don't create
something like:

<business xmlns:xmlns="http:'www.abcd1234.com/rent"></business>

Don't use xmlns as your default namespace

To own a URL (such as a website), you must register a domain
name with an authority (such as Network Solutions or GoDaddy, etc). Once you own
the name, you can use it as you see fit. For example, inside your website, you
can create directories and sub-directories any way you want. To communicate a
URL to other people, you give the address. When it comes to namespaces, you can
also create your own, using a domain name you will have registered. As an
alternative, you can use namespaces that other people have created. As mentioned
above the W3C organization provides a few namespaces already. Microsoft also
provides many already created namespaces. We will come back to them.

Using a Namespace

After creating a namespace, you can use the elements to
which it refers. When creating an XML element, precede the tag's name with a
prefix of the namespace and a colon ":". After the name of the tag, define the namespace
that contains xmlns: followed by the prefix and assign the namespace's URL to
it. Here is an example:

If you want to create a tag that has an attribute,
after the name of the tag, type the prefix you had defined for the namespace,
followed by :, followed by the attribute's name and assign the desired value to
it. Optionally, you can assign the desired value to the attribute. If necessary,
close the tag and optionally specify its value. Here is an example:

To access one of the names, you must qualify it. To do this,
type the name of the prefix, followed by :, and followed by the name of the
element.

Introduction to XML Schemas

Definition

We know how to create XML
elements and how to add values to them. Some values appeared as regular strings.
Some appeared as number, and so on. In reality, we did not
define what type each value was. In fact, we could have a child node whose value
appeared as a string while the same child node in the element would appear as a
numbers. An XML schema is a document that specifies the
type of value that each element of the corresponding XML document uses. This
means that an XML schema must provide at least two pieces of information: the XML
document that the schema refers to and the list of elements defined by the
schema.

Introduction to Creating an XML Schema

Like an XML file, an XML schema is a text-based document
made of tags. You can start the document with a document type definition (DTD)
that including encoding:

<?xml version="1.0" encoding="utf-8"?>

After that line, you will create the elements of the
document. The first characteristic of an XML schema document is that it must
specify a namespace. The namespace is defined in a document that specifies the
types of values used in the document. As seen in our introduction to namespaces,
you must decide about the prefix you
will use in the various tags of the document. By convention, most people use
either xs, which stands of XML Schema, or xsd, which stands for XML Schema
Definition. Here is an example that uses xs:

<?xml version="1.0" encoding="utf-8"?>
<xs>

As mentioned in our review of namespaces, the prefix must be
followed by a colon and a name for the tag. In a typical XML schema, the first
tag is named schema:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema>

As mentioned already, you must specify the namespace you
will use. As mentioned in our study of namespaces, you create an attribute what
uses xmlns as its prefix followed by a column and the prefix previously defined:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs>

You must assign the URL of the namespace to that attribute.

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="">

After creating the document, you should (must) save it and
give it a name. An XML schema file has the extension .xsd.

Built-In Schemas

As mentioned already, the primary role of an XML schema is
to define the types of values used in an XML document. There are various types
of values that can be used in an XML document. Instead of defining your own
types of values, many people and companies have already created XML schemas that
you can simply use in your project. We refer to these as built-in XML schemas.

Among others, both the W3C consortium and Microsoft have
already created XML schemas you can use. To see a list of built-in XML schemas,
if you are working in Microsoft Visual Studio, first create or open an XML file.
On the main menu, click XML -> Schemas... This would open the XML Schemas
dialog box

To use any of these schemas, assign it to your xmlns:Prefix attribute. Here is an example:

Notice that, in the XML Schemas dialog box, you can add a new one by clicking Add, locating, and
then selecting the file.

Introduction to Creating a Tag in an XML Schema

Consider the following XML document:

<?xml version="1.0" encoding="utf-8"?>
<video />

As mentioned already, to create its corresponding XML
schema, start a text-based document that contains a DTD and a tag that specifies
the foundational namespace. Of course, this tag must be closed. Here is an
example:

Between the start and the end tags, you must specify the
necessary tag. Since the schema is tied to the XML tag, it must include a tag
for each of the XML elements. Each tag must start with the prefix, a column, and
the keyword element. This would be done as follows:

In programming languages, a data type is a word or a group
of words that indicates the amount of computer memory necessary to store a
certain value or a certain kind of value. A value can be considered as a
character, a group of characters (also called a string), a number, a date, etc.
In XML, these are referred to as simple types. Consider the following XML
document:

Notice that it contains only one element, that element has a
value, and it does not have any child node. One of the rules of simple types is
that their correspond node cannot have a child element. In the same way, an
element of simple type cannot have an attribute.

Instead of defining your own simple types, you can use a
built-in schema that already has the definitions you will need and you can
simply use its data types. To specify the data type used by an element, in the
XML schema and in the start-tag of the element, create an attribute whose name
is type. Assign a string that contains the name of the prefix,
followed by a colon, and followed by the data type. This would be done as
follows:

As mentioned already, a data type is primarily represented
by a name but it may also have some internal characteristics that express how
the data type functions. A number is referred to as signed if it can be
represented with a leading sign as + or -, which means the number can be
positive (from 0 up) or negative (from 0 down). When no leading sign is used,
the number is considered positive. A number is reffered to as unsigned if it is
positive.

The most common data types used for values of XML
elements are:

string: This is used for a letter (or a symbol) or a
group of letters (or a group of letters or a combination of letters and
symbols, readable or non-readable). Here is an example:

boolean: A Boolean value is one that can be considered true (or 1) or false (or 0)

byte: If an XML
element uses small signed natural numbers
between -128 and 127, you can set its data type as byte.As is the case for all numbers, the value can be represented in normal
format (such as -4 or 18 or 105) or in scientific notation, which is in the form a x 10b

unsignedByte: The unsigned byte type is for elements that use
small positive numbers in the range of 0 to 255

short: A short integer is a positive or negative natural number
that is between -32768 and 32767. For an element that uses that type of
number, use the short data type

unsignedShort: A number unsigned

int: The int data type is used by elements whose value can
be positive or negative natural number, ranging from -2147483648 to
2147483647

unsignedInt: This is called unsigned integer. It is appropriate
for elements that use a natural positive number that can range from 0 to 4294967295

integer: An integer is large natural number. This type
allows negative or positive values. A negative value uses a leading - sign,
as in -3972594. If no leading sign is used, the value is considered positive

negativeInteger: This type is for XML elements that use
negative natural numbers starting at -1 down to -∞

nonNegativeInteger: This is for negative natural numbers starting at
0 down to -∞

positiveInteger: As its name indicates, this data type
is used for elements whose values must always be positive, starting at 1 up
to ∞

nonPositiveInteger: This is for positive natural numbers starting at
0 up ∞

long: This long integer data type is used for very
large natural numbers that range from -9223372036854775808 to
9223372036854775807. Remember that you can write the number is scientific
format to make it easier to read

unsignedLong: This data type is used for long integers
that must always be positive, ranging from 0 to 18446744073709551615

decimal: A decimal is a number that represents a
fraction. It is made of two parts separated by a period. The integral part
represents a whole. The number on the right side of the period represents a
fraction of a 10

float: The float data type is used as a decimal number for
elements that don't require much precision, or precision referred to as
single

double: If an element uses decimal numbers that require much
precision, set their data type to double

date: This data type is reserved for date-based values. The value
can be set using the following format: YYYY-MM-DD:

YYYY represents 4 digits for the year

MM represents the digits for the month. If the value is less than
10, you can use or omit the leading 0

DD represents the digits for the day. If the value is less than 10,
you can use or omit the leading 0

time: This data type is for time-based values. The value can
should be specified as 00:00:00.000 where:

The first two 0s represent the hours. If the hour value is less than
10, you use or omit the leading 0

The second two 0s represent the minutes. If the value is less than
10, you can use or omit the leading 0

The third combination of 0s represent the seconds. If the is less
than 10, you can use or omit the leading 0

The three 0s represent the miliseconds

datetime: This type is for a combination of date and time using
one of the following formulas:

YYYY-MM-DDT00:00:00.000 where YYYY, MM, and DD are the same as for
the date data type. The T must for be used to separate the date
and the time. 00:00:00.000 are the same as reviewed for the time data
type

YYYY-MM-DDT00:00:00.000-00:00 where all values are as described
above. The addition here is -00:00. This value is used to set the
difference from the universal time

Notice that the video element has two child elements named
title and director. When an XML element has a child element, that parent is said
to have a complex type. In your XML schema, to create a tag for a parent
element, create a tag named complexType and close it. This would be done as
follows:

Between the start and end-tags of the complex type, you will create the tag(s)
for the child element. But first, you must indicate that you are about to
specify the order of the child elements. This is done by creating a tag named
sequence. This can be done as follows:

Notice that the actors element, which is nested in the video
element, has children elements of its own. For any node that has at least one child node,
create a complexType start-tag and close it. Inside this complex type,
create a sequence start-tag and close it. Inside the new sequence
tag, create an element tag for each child element and specify its data
type. Here is an example:

As you should know already, once an element has an
attribute, it is said to have a complex type. Therefore, before creating a
schema tag for the attribute, you must first create a complex parent type. Then
create a child tag. Instead of the element keyword, use
attribute. Here is an example:

In the same way, if a child element has an attribute, first
create a complexType start-tag for it and close it. Inside this complex type,
create a sequence start-tag and close it. Inside the new sequence
tag, create an attribute tag for each attribute and specify its data
type.

Options on Tags of an XML Schema

The Default Value of an Element or Attribute

By default, when creating an element or an attribute, you
can give it any value you want. In fact, you create an element on one section of
the document and give it a value, then create the same element in another
section of the document but not give it a value. Here is an example:

If you have an element or an attribute that usually receive
the same value, you can specify a default value for it. To do this, when creating
the schema of the element or attribute, add a flag named default and
assign the desired value to it. Here is an example:

Notice that the product element has two child nodes. When
creating an XML document, you must want to require that the a certain element
have at least a certain number of child elements. In that case, if the element
has less than that number of child elements, the document would be in violation
of the rule and therefore would be valid.

To specify that an element must have at least a certain
number of child node, when creating its schema tag, add an attribute named
minOccurs and assign the desired number to it. Here is an example:

This code indicates that the products element must have at
least 4 child nodes. Since the products element in the above XML document has
only two child nodes, the element violates the rule and would produce an error.

The Maximum Number of Occurrences of an Element

When creating an XML document, by default, you can create as
many elements as you want and each element can have as many child nodes as
necessary. If you want, you can limit the number of child nodes an element can
have. To do this, when creating the schema tag of the element, add an attribute
named maxOccurs and assign the desired value to it. The value
can be either an insigned integer or unbounded. This can be done as
follows:

In this case, the products element must not have more than 4
child nodes. In the XML document, if you create more child nodes for the element than the number specified in maxOccurs,
the document would be violating the riule and would be considered invalid.

Since the minOccurs and the maxOccurs flags
are not mutually exclusive, you can use them both on the same element. Here is
an example:

Notice that the first product element has an attribute named
number while the second product element does not have that attribute. When
creating the schema of an element, you can ask that a certain attribute be
required. To take care of this,
when creating the schema of the attribute, add an attribute named use and assign a value
named required to it. Here is an example:

Once this flag is set, whenever the element of that
attribute is created, that attribute must be added. Otherwise, the element would
violate the rule.

Setting an Attribute as Optional

Once again, consider the above XML document. Notice that the first product element has an attribute named
number but the second product element does not have that attribute. When
creating the schema of an element, you can add a certain attribute to a certain
element but omit that attribute
for the same element in another section of the document. In this cas, the
attribute is said to be optional. To indicate this, when creating the schema of the attribute, add
the use attribute and assign a value
named optional to it. Here is an example:

If you want to prevent the use of an element from a certain
element in your XML document, create its tag in the XML schema document. Add
both the minOccurs and the maxOccurs attributes and set the value
of each to 0. Here is an example:

Notice that the first gender element has a value of Male and
the second has a value of Female. We could have created one more gender element
and give it any value. In a typical XML document, especially in the absence of a
schema, you can give any value to an element. In some cases, you can specify a
constant value that must always be used for an element. To do this, when
creating the schema tag of the element, add an attribute named fixed and
assign the desired value to it. Here is an example:

Once this is done, if a new element is created and it uses a
value other than the one specified in the fixed flag, the document would
be violating the rule.

The fixed flag can also be applied to an attribute,
in which case you would require that an attribute always carry a certain value.
In fact, you can add the use flag and specify whether the attribute must
be required or it would be optional.