System.Xml.Schema.XmlSchemaElement Class

Assembly: System.Xml.dll
Namespace: System.Xml.Schema
Summary
Class for elements. Represents the World Wide Web Consortium (W3C) element element.
C# Syntax:
public class XmlSchemaElement : XmlSchemaParticle
Example
The following example creates the element element.
using System;
using System.Xml;  
using System.Xml.Schema;

class XMLSchemaExamples {
	public static void Main() {
 
		XmlSchema schema = new XmlSchema();
		
		// <xs:element name="cat" type="string"/>
		XmlSchemaElement elementCat = new XmlSchemaElement();
		schema.Items.Add(elementCat);
		elementCat.Name = "cat";
		elementCat.SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
		
		// <xs:element name="dog" type="string"/>
		XmlSchemaElement elementDog = new XmlSchemaElement();
		schema.Items.Add(elementDog);
		elementDog.Name = "dog";
		elementDog.SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");

		// <xs:element name="redDog" substitutionGroup="dog" />
		XmlSchemaElement elementRedDog = new XmlSchemaElement();
		schema.Items.Add(elementRedDog);
		elementRedDog.Name = "redDog";
		elementRedDog.SubstitutionGroup = new XmlQualifiedName("dog");
		
		
		// <xs:element name="brownDog" substitutionGroup ="dog" />
		XmlSchemaElement elementBrownDog = new XmlSchemaElement();
		schema.Items.Add(elementBrownDog);
		elementBrownDog.Name = "brownDog";
		elementBrownDog.SubstitutionGroup = new XmlQualifiedName("dog");
		
		
		// <xs:element name="pets">
		XmlSchemaElement elementPets = new XmlSchemaElement();
		schema.Items.Add(elementPets);
		elementPets.Name = "pets";
		
		// <xs:complexType>
		XmlSchemaComplexType complexType = new XmlSchemaComplexType();
		elementPets.SchemaType = complexType;
		
		// <xs:choice minOccurs="0" maxOccurs="unbounded">
		XmlSchemaChoice choice = new XmlSchemaChoice();
		complexType.Particle = choice;
		choice.MinOccurs = 0;
		choice.MaxOccursString = "unbounded";
		
		// <xs:element ref="cat"/>
		XmlSchemaElement catRef = new XmlSchemaElement();
		choice.Items.Add(catRef);
		catRef.RefName = new XmlQualifiedName("cat");
		
		// <xs:element ref="dog"/>
		XmlSchemaElement dogRef = new XmlSchemaElement();
		choice.Items.Add(dogRef);
		dogRef.RefName = new XmlQualifiedName("dog");
		
        schema.Compile(new ValidationEventHandler(ValidationCallbackOne));
 	XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable());
        nsmgr.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema");
        schema.Write(Console.Out, nsmgr);
	}

    public static void ValidationCallbackOne(object sender, ValidationEventArgs args) {
        Console.WriteLine(args.Message);
    }
}

    

The following XML file is used for the preceding code example.

<?xml version="1.0" encoding="IBM437"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
	<xs:element name="cat" type="xs:string"/>
	<xs:element name="dog" type="xs:string"/>
	<xs:element name="redDog" substitutionGroup="dog" />
	<xs:element name="brownDog" substitutionGroup ="dog" />

	<xs:element name="pets">
	  <xs:complexType>
	    <xs:choice minOccurs="0" maxOccurs="unbounded">
	      <xs:element ref="cat"/>
	      <xs:element ref="dog"/>
	    </xs:choice>
	  </xs:complexType>
	</xs:element>
</xs:schema>

    
See also:
System.Xml.Schema Namespace

System.Xml.Schema.XmlSchemaElement Member List:

Public Constructors
ctor #1 Default constructor. This constructor is called by derived class constructors to initialize state in this type.
Public Properties
Annotation
(inherited from System.Xml.Schema.XmlSchemaAnnotated)
Read-write

See base class member description: System.Xml.Schema.XmlSchemaAnnotated.Annotation


Gets or sets the annotation property.
Block Read-write

Gets or sets a Block derivation.
BlockResolved Read-only

Gets the post-compilation value of the Block property which holds the post-compilation value of the Block property.
Constraints Read-only

Gets the collection of constraints on the element.
DefaultValue Read-write

Gets or sets the default value of the element if its content is a simple type or content of the element is textOnly.
ElementType Read-only

Gets a common language runtime (CLR) object based on the XmlSchemaElement or XmlSchemaElement of the element which holds the post-compilation value of the ElementType property.
Final Read-write

Gets or sets the Final property to indicate that no further derivations are allowed.
FinalResolved Read-only

Gets the post-compilation value of the Final property which holds the post-compilation value of the Final property.
FixedValue Read-write

Gets or sets the fixed value.
Form Read-write

Gets or sets the form for the element.
Id
(inherited from System.Xml.Schema.XmlSchemaAnnotated)
Read-write

See base class member description: System.Xml.Schema.XmlSchemaAnnotated.Id


Gets or sets the string id.
IsAbstract Read-write

Gets or sets information to indicate if the element can be used in an instance document.
IsNillable Read-write

Gets or sets information that indicates if xsi:nil can occur in the instance data. Indicates if an explicit nil value can be assigned to the element.
LineNumber
(inherited from System.Xml.Schema.XmlSchemaObject)
Read-write

See base class member description: System.Xml.Schema.XmlSchemaObject.LineNumber


Gets or sets the line number in the file to which the schema element refers.
LinePosition
(inherited from System.Xml.Schema.XmlSchemaObject)
Read-write

See base class member description: System.Xml.Schema.XmlSchemaObject.LinePosition


Gets or sets the line position in the file to which the schema element refers.
MaxOccurs
(inherited from System.Xml.Schema.XmlSchemaParticle)
Read-write

See base class member description: System.Xml.Schema.XmlSchemaParticle.MaxOccurs


Gets or sets the maximum number of times the particle can occur.
MaxOccursString
(inherited from System.Xml.Schema.XmlSchemaParticle)
Read-write

See base class member description: System.Xml.Schema.XmlSchemaParticle.MaxOccursString


Gets or sets the number as a string value. Maximum number of times the particle can occur.
MinOccurs
(inherited from System.Xml.Schema.XmlSchemaParticle)
Read-write

See base class member description: System.Xml.Schema.XmlSchemaParticle.MinOccurs


Gets or sets the minimum number of times the particle can occur.
MinOccursString
(inherited from System.Xml.Schema.XmlSchemaParticle)
Read-write

See base class member description: System.Xml.Schema.XmlSchemaParticle.MinOccursString


Gets or sets the number as a string value. The minimum number of times the particle can occur.
Name Read-write

Gets or sets the name of the element.
Namespaces
(inherited from System.Xml.Schema.XmlSchemaObject)
Read-write

See base class member description: System.Xml.Schema.XmlSchemaObject.Namespaces


QualifiedName Read-only

Gets the actual qualified name for the given element. This is the name that a ref attribute refers to. The qualified name consists of the NCName of the element. For example, element name = "NCName", and the prefix used to describe the targetNamespace of the schema. This property holds the post-compilation value of the QualifiedName property.
RefName Read-write

Gets or sets the reference name of an element declared in this schema (or another schema indicated by the specified namespace).
SchemaType Read-write

Gets or sets the type of the element. This can either be a complex type or a simple type.
SchemaTypeName Read-write

Gets or sets the name of a built-in data type defined in this schema or another schema indicated by the specified namespace.
SourceUri
(inherited from System.Xml.Schema.XmlSchemaObject)
Read-write

See base class member description: System.Xml.Schema.XmlSchemaObject.SourceUri


Gets or sets the source location for the file that loaded the schema.
SubstitutionGroup Read-write

Gets or sets the name of an element that is being substituted by this element.
UnhandledAttributes
(inherited from System.Xml.Schema.XmlSchemaAnnotated)
Read-write

See base class member description: System.Xml.Schema.XmlSchemaAnnotated.UnhandledAttributes


Gets or sets the qualified attributes that do not belong to the schema target namespace.
Public Methods
Equals
(inherited from System.Object)
See base class member description: System.Object.Equals

Derived from System.Object, the primary base class for all objects.
GetHashCode
(inherited from System.Object)
See base class member description: System.Object.GetHashCode

Derived from System.Object, the primary base class for all objects.
GetType
(inherited from System.Object)
See base class member description: System.Object.GetType

Derived from System.Object, the primary base class for all objects.
ToString
(inherited from System.Object)
See base class member description: System.Object.ToString

Derived from System.Object, the primary base class for all objects.
Protected Methods
Finalize
(inherited from System.Object)
See base class member description: System.Object.Finalize

Derived from System.Object, the primary base class for all objects.
MemberwiseClone
(inherited from System.Object)
See base class member description: System.Object.MemberwiseClone

Derived from System.Object, the primary base class for all objects.

Hierarchy:


System.Xml.Schema.XmlSchemaElement Member Details

ctor #1
Summary:
Default constructor. This constructor is called by derived class constructors to initialize state in this type.
C# Syntax:
public XmlSchemaElement();

Return to top


Property: Annotation (read-write)
Inherited
See base class member description: System.Xml.Schema.XmlSchemaAnnotated.Annotation

Summary
Gets or sets the annotation property.
C# Syntax:
public XmlSchemaAnnotation Annotation {get; set;}

Return to top


Property: Block (read-write)
Summary
Gets or sets a Block derivation.
C# Syntax:
public XmlSchemaDerivationMethod Block {get; set;}
Remarks
This attribute can contain the following values:

Enum Description
None Use the derivation restriction defined by the BlockDefault property of the schema, if it exists.
Empty There are no restrictions on replacements of this element.
Substitution This element cannot be replaced by a type derived by Substitution.
Extension This element cannot be replaced by a type derived by Extension.
Restriction This element cannot be replaced by a type derived by Restriction.
All #all . This element cannot be replaced by any type regardless of derivation method.

This property does not prevent derivation but prevents derivation from replacing the type in an instance. List and union values are not supported for the block property.

Return to top


Property: BlockResolved (read-only)
Summary
Gets the post-compilation value of the Block property which holds the post-compilation value of the Block property.
C# Syntax:
public XmlSchemaDerivationMethod BlockResolved {get;}
Remarks
This value indicates how the type is enforced when xsi:type is used in the instance document. For example, if the value is restricted, only the specific defined type can be used, not types derived by restriction from the specific defined type. This property can contain the values shown in the following table. The default is None. List and union derivation methods are not allowed on this property.

Enum Description
Empty There are no restrictions on replacements of this element.
Substitution This element cannot be replaced by a type derived by Substitution.
Extension This element cannot be replaced by a type derived by Extension.
Restriction This element cannot be replaced by a type derived by Restriction.
All #all . This element cannot be replaced by any type regardless of derivation method.

Return to top


Property: Constraints (read-only)
Summary
Gets the collection of constraints on the element.
C# Syntax:
public XmlSchemaObjectCollection Constraints {get;}
Remarks
Returns a collection of constraints on the element. Can be any of XmlSchemaKey, XmlSchemaKeyref, or XmlSchemaUnique.

Return to top


Property: DefaultValue (read-write)
Summary
Gets or sets the default value of the element if its content is a simple type or content of the element is textOnly.
C# Syntax:
public string DefaultValue {get; set;}
Remarks
The fixed and default attributes are mutually exclusive. If the element contains a simple type, this value must be a valid value of that type.

Return to top


Property: ElementType (read-only)
Summary
Gets a common language runtime (CLR) object based on the XmlSchemaElement or XmlSchemaElement of the element which holds the post-compilation value of the ElementType property.
C# Syntax:
public object ElementType {get;}
Remarks
The object can be XmlSchemaDatatype if the element has a built-in XML Schema type or XmlSchemaSimpleType if the element has a user-defined type.

This property is a post-schema-validation infoset property.

Return to top


Property: Final (read-write)
Summary
Gets or sets the Final property to indicate that no further derivations are allowed.
C# Syntax:
public XmlSchemaDerivationMethod Final {get; set;}
Remarks
This attribute can contain the values shown in the following table.

Enum Description
None Use the derivation restriction defined by the FinalDefault property of the schema, if it exists.
Empty There are no restrictions on derivations of this element.
Extension This element cannot be derived by Extension.
Restriction This element cannot be derived by Restriction.
All #all . This element cannot be derived by any derivation method.
See also XmlSchemaElement.Substitution, List, and Union derivation methods are not allowed for this property.

Return to top


Property: FinalResolved (read-only)
Summary
Gets the post-compilation value of the Final property which holds the post-compilation value of the Final property.
C# Syntax:
public XmlSchemaDerivationMethod FinalResolved {get;}
Remarks
This value indicates how the type is restricted when the type is extended through restriction. This property can contain the following values.

Enum Description
Empty There are no restrictions on derivations of this element.
Extension This element cannot be derived by Extension.
Restriction This element cannot be derived by Restriction.
All #all . This element cannot be derived by any derivation method.
Substitution, List, and Union derivation methods are not allowed for this property.

Return to top


Property: FixedValue (read-write)
Summary
Gets or sets the fixed value.
C# Syntax:
public string FixedValue {get; set;}
Remarks
The content should be a simple type or textOnly. The fixed and default attributes are mutually exclusive.

Return to top


Property: Form (read-write)
Summary
Gets or sets the form for the element.
C# Syntax:
public XmlSchemaForm Form {get; set;}
Remarks
The default value is the value of the elementFormDefault attribute for the schema element containing the element. The default is Unqualified.

Enum Description
Qualified Element must be qualified with the namespace prefix.
Unqualified Element is not required to be qualified with the namespace prefix.

The value cannot be set if the containing element is the schema element.

Return to top


Property: Id (read-write)
Inherited
See base class member description: System.Xml.Schema.XmlSchemaAnnotated.Id

Summary
Gets or sets the string id.
C# Syntax:
public string Id {get; set;}
Remarks
The id value must be of type ID and be unique within the document containing this element.

Optional.

Return to top


Property: IsAbstract (read-write)
Summary
Gets or sets information to indicate if the element can be used in an instance document.
C# Syntax:
public bool IsAbstract {get; set;}

Return to top


Property: IsNillable (read-write)
Summary
Gets or sets information that indicates if xsi:nil can occur in the instance data. Indicates if an explicit nil value can be assigned to the element.
C# Syntax:
public bool IsNillable {get; set;}
Remarks
This applies to element content and not the attributes of the element.

Return to top


Property: LineNumber (read-write)
Inherited
See base class member description: System.Xml.Schema.XmlSchemaObject.LineNumber

Summary
Gets or sets the line number in the file to which the schema element refers.
C# Syntax:
public int LineNumber {get; set;}
Remarks
LineNumber is used to store the line number when a schema is read from a file. This is reported through XmlSchemaException for error handling.

Return to top


Property: LinePosition (read-write)
Inherited
See base class member description: System.Xml.Schema.XmlSchemaObject.LinePosition

Summary
Gets or sets the line position in the file to which the schema element refers.
C# Syntax:
public int LinePosition {get; set;}
Remarks
This is used to store the line position when a schema is read from a file. This is reported through XmlSchemaException for error handling.

Return to top


Property: MaxOccurs (read-write)
Inherited
See base class member description: System.Xml.Schema.XmlSchemaParticle.MaxOccurs

Summary
Gets or sets the maximum number of times the particle can occur.
C# Syntax:
public decimal MaxOccurs {get; set;}
Remarks
The value can be an integer greater than or equal to zero.

Return to top


Property: MaxOccursString (read-write)
Inherited
See base class member description: System.Xml.Schema.XmlSchemaParticle.MaxOccursString

Summary
Gets or sets the number as a string value. Maximum number of times the particle can occur.
C# Syntax:
public string MaxOccursString {get; set;}
Remarks
This property can have a null ("") value or String.Empty.

To set no limit on the maximum number, use the string "unbounded".

Return to top


Property: MinOccurs (read-write)
Inherited
See base class member description: System.Xml.Schema.XmlSchemaParticle.MinOccurs

Summary
Gets or sets the minimum number of times the particle can occur.
C# Syntax:
public decimal MinOccurs {get; set;}
Remarks
The value can be an integer greater than or equal to zero. Set this attribute to zero to indicate that the attribute is optional.

Return to top


Property: MinOccursString (read-write)
Inherited
See base class member description: System.Xml.Schema.XmlSchemaParticle.MinOccursString

Summary
Gets or sets the number as a string value. The minimum number of times the particle can occur.
C# Syntax:
public string MinOccursString {get; set;}
Remarks
This property can have a null ("") value or String.Empty.

Return to top


Property: Name (read-write)
Summary
Gets or sets the name of the element.
C# Syntax:
public string Name {get; set;}
Remarks
The name must be an NCName (no-colon name) as defined in the World Wide Web Consortium (W3C) XML Namespaces specification.Name and RefName attributes are mutually exclusive.

Return to top


Property: Namespaces (read-write)
Inherited
See base class member description: System.Xml.Schema.XmlSchemaObject.Namespaces

C# Syntax:
public XmlSerializerNamespaces Namespaces {get; set;}

Return to top


Property: QualifiedName (read-only)
Summary
Gets the actual qualified name for the given element. This is the name that a ref attribute refers to. The qualified name consists of the NCName of the element. For example, element name = "NCName", and the prefix used to describe the targetNamespace of the schema. This property holds the post-compilation value of the QualifiedName property.
C# Syntax:
public XmlQualifiedName QualifiedName {get;}

Return to top


Property: RefName (read-write)
Summary
Gets or sets the reference name of an element declared in this schema (or another schema indicated by the specified namespace).
C# Syntax:
public XmlQualifiedName RefName {get; set;}
Remarks
The RefName must be a QName. The RefName can include a namespace prefix. If RefName attribute is present, all complexType, simpleType, key, keyref, unique, nillable, default, fixed, form, block, and type must be absent. If RefName is present, minOccurs, maxOccurs, and id are allowed. To declare an element using an existing element definition, use the RefName attribute to specify the existing element definition.

The value cannot be set if the containing element is the schema element.

Return to top


Property: SchemaType (read-write)
Summary
Gets or sets the type of the element. This can either be a complex type or a simple type.
C# Syntax:
public XmlSchemaType SchemaType {get; set;}
Remarks
To declare an element using an existing simpleType or complexType definition, use the SchemaType property to specify the existing type. If RefName attribute is present, complexType, simpleType, key, keyref, and unique elements and nillable, default, fixed, form, block, and SchemaType attributes cannot be present.

Return to top


Property: SchemaTypeName (read-write)
Summary
Gets or sets the name of a built-in data type defined in this schema or another schema indicated by the specified namespace.
C# Syntax:
public XmlQualifiedName SchemaTypeName {get; set;}

Return to top


Property: SourceUri (read-write)
Inherited
See base class member description: System.Xml.Schema.XmlSchemaObject.SourceUri

Summary
Gets or sets the source location for the file that loaded the schema.
C# Syntax:
public string SourceUri {get; set;}
Remarks
Provides information for exception handling.

Return to top


Property: SubstitutionGroup (read-write)
Summary
Gets or sets the name of an element that is being substituted by this element.
C# Syntax:
public XmlQualifiedName SubstitutionGroup {get; set;}
Remarks
This element must have the same type or a type derived from the type of the specified element. This value must be a QName.

The value cannot be set if the containing element is the schema element.

Return to top


Property: UnhandledAttributes (read-write)
Inherited
See base class member description: System.Xml.Schema.XmlSchemaAnnotated.UnhandledAttributes

Summary
Gets or sets the qualified attributes that do not belong to the schema target namespace.
C# Syntax:
public XmlAttribute[] UnhandledAttributes {get; set;}

Return to top


Method: Equals(
   object obj
)
Inherited
See base class member description: System.Object.Equals
C# Syntax:
public virtual bool Equals(
   object obj
);

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~XmlSchemaElement();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetHashCode()
Inherited
See base class member description: System.Object.GetHashCode
C# Syntax:
public virtual int GetHashCode();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: ToString()
Inherited
See base class member description: System.Object.ToString
C# Syntax:
public virtual string ToString();

For more information on members inherited from System.Object click on the link above.

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.