This documentation is archived and is not being maintained.

XML Elements, Attributes, and Types (XML Designer) 


In versions prior to Visual Studio 2005, the XML Designer is the designer for typed datasets and XML schema. The XML Designer can still be used to create and edit XML schema files. However, in the current version, the designer for creating and editing typed datasets is the Dataset Designer. For more information, see Dataset Designer.

This page assumes you are working with the XML Designer. To open a typed dataset file with the XML Designer, right-click the file in Solution Explorer, point to Open With, and then click XML Schema Editor.

The basic building blocks of XML Schemas are elements and attributes. Data types define the valid content that elements and attributes contain. When you create XML Schemas, you define the individual elements and attributes and assign valid types to them. Elements describe data, whereas attributes are like properties of an element, in that they provide further definition about the element the way that properties describe characteristics of objects and classes.


An element describes the data that it contains. Elements can also contain other elements and attributes. For more information, see element.

When an element definition contains additional elements or attributes, it is a complex type. For more information on complex types, see the "Types" section later in this topic.

A basic element definition consists of a name and a data type. The following example shows how to define an element named quantity, with a simple type value of integer.

<xs:element name="quantity" type="xs:integer" />

The element classifies the data — in this case as a quantity. The type defines the valid content that the element contains — in this case an integer.

The following example shows a valid instance of XML data that conforms to the quantity element defined in the previous example:


The following example shows an invalid instance of the quantity element:

<quantity>sixty three</quantity>


An attribute is a named simple-type definition that cannot contain other elements. Attributes can also be assigned an optional default value and they must appear at the bottom of complex-type definitions. Additionally, if multiple attributes are declared, they may occur in any order. For more information, see attribute.

The following code shows how to declare an attribute named OrderDiscount that is defined with the simple type number. The use of an attribute makes sense here since attributes are optional. If no OrderDiscount is provided the XML data will still be valid.

   <xs:element name="OrderInfo">
            <xs:element name="CustomerName" type="xs:string" />
            <xs:element name="OrderNumber" type="xs:positiveInteger" />
            <xs:element name="OrderTotal" type="xs:number" />
         <xs:attribute name="OrderDiscount" type="xs:number" />

Naming Elements and Attributes

The following are a few relevant details about elements and attributes:

  • XML is case-sensitive. Thus an element named "statecode" is different from an element named "stateCode."

  • Attribute values must always be in quotation marks.

  • Element names cannot start with a number or underscore, or with the letters "XML."

  • Element names cannot contain spaces.

Should I Use an Element or an Attribute?

Elements are used to encapsulate pieces of data, and attributes are generally used to provide accompanying information about an element, rather than to encapsulate raw data itself. Whether you actually use an element or an attribute is up to the needs of your application.

Use attributes if your information requires data of a simple type and:

  • Your information requires a default or fixed value.

  • Your information requires data that is metadata for an existing element

  • If the size of your XML file is an issue, attributes tend to take up fewer bytes than elements

The following list describes the key differences between elements and attributes from a schema perspective:

  • A schema can define whether the ordering of elements is significant, but attributes can occur in any order.

  • Elements can be nested with the <choice> tag, which means one and only one of the listed elements can appear.

  • Elements can occur more than once, but attributes cannot.


Types in XML Schemas define the valid data type that elements or attributes may contain, and can be simple or complex. Additionally, types can be named or unnamed. For more information, see Named vs. Unnamed Types and Groups.

Simple Types

There are two major categories of simple types:

  • Built-in types, which are defined by the World Wide Web Consortium's XML Schema specification - for example, string, boolean, and float.

    Built-in types include both primitive types and derived types.

    Primitive data types are not derived from other data types. For example, float is a mathematical concept that is not derived from other data types. For more information, see Primitive XML Data Types.

    Derived data types are defined in terms of existing data types. For example, integer is a special case derived from the decimal data type. For more information, see Derived XML Data Types.

  • User-defined simple types are derived from the W3C built-in types by applying user-defined values to items called facets. For more information, see User-Defined Simple Types in XML Schemas.


Facets constrain the acceptable values of simple types. User-defined simple types can be created by applying facets. For more information, see Data Type Facets.

In the following example the maxInclusive facet is applied to a simple type named qtyLimiter in order to restrict the acceptable values of the positiveInteger type to quantities from 1 through 100:

<xs:simpleType name="qtyLimiter">
   <xs:restriction base="xs:positiveInteger">
      <xs:maxInclusive value="100" />

Complex Types

Complex types are element definitions that can include other elements, attributes, and groups. An important difference between complex and simple types is that complex types can include elements and attributes declared as simple or complex types, whereas simple types cannot include other elements, attributes, or groups but can only include facets. For more information, see Complex Types in XML Schemas.

See Also