In This Article

Context

The XML language can return detailed context information about any offset in a document. The context includes data such as declared namespaces, containing element hierarchy, current element/attribute, and more. This sort of information is essential in determining what to display in automated IntelliPrompt.

Obtaining Context Information

Context information can be retrieved via the use of the XmlContextFactory class. Its CreateContext method accepts a TextSnapshotOffset and returns the IXmlContext for that offset.

This code retrieves an IXmlContext for the caret's offset (the end selection offset in the active view):

TextSnapshotOffset snapshotOffset = syntaxEditor.ActiveView.Selection.EndSnapshotOffset;
IXmlContext context = new XmlContextFactory().CreateContext(snapshotOffset);

XML Context

IXmlContext objects provide XML-related context information for a specific snapshot offset, such as the element hierarchy that contains the offset, the available namespace declarations, current element/attribute, and more.

These IXmlContext members are important:

Member Description
ElementHierarchy Property Gets a read-only list of IXmlElementContext objects that describes the element hierarchy. The element hierarchy is the list of parent elements that contain the current offset.
InitializationSnapshotRange Property Gets the TextSnapshotRange with which the context was initialized.
NamespaceDeclarations Property Gets the mapping dictionary of namespace to namespace prefixes that are declared at the context. The dictionary keys are namespaces and the values are namespace prefixes.
SchemaResolver Property Gets the IXmlSchemaResolver to use.
SnapshotOffset Property Gets the TextSnapshotOffset for which this context was created.
TargetAttribute Property Gets the IXmlAttributeContext that contains context information related to the target attribute. This property is only populated if the context is related to an attribute.
TargetElement Property Gets the IXmlElementContext that contains context information related to the target element. This property is only populated if the context is related to an element or attribute.
Type Property Gets an XmlContextType that specifies the type of context.

Element Context

IXmlContext objects can return contextual data about XML elements via their ElementHierarchy and TargetElement properties. Both of these properties return objects of type IXmlElementContext that have element contextual information.

Element contexts contain their namespace/name information, included attributes, resolved schema element data, and various snapshot ranges.

These IXmlElementContext members are important:

Member Description

Attributes Property

Gets the dictionary of attributes for this element. The dictionary keys are the attribute qualified names and the values are IXmlAttributeContext objects.

EndTagSnapshotRange Property

Gets the nullable TextSnapshotRange that specifies the range of the end tag, if there is one.

Name Property

Gets the name of the element.

Namespace Property

Gets the namespace that defines the element.

NamespacePrefix Property

Gets the prefix of the namespace that defines the element.

QualifiedName Property

Gets the XmlQualifiedName that contains the element's qualified name.

SchemaElement Property

Gets the resolved XmlSchemaElement for this element, if known.

StartTagNameSnapshotRange Property

Gets the TextSnapshotRange that specifies the range of the start tag name.

Attribute Context

IXmlAttributeContext objects specify contextual information about XML attributes. Instances are returned by the IXmlContext.TargetAttribute and IXmlElementContext.Attributes properties.

Attribute contexts contain their namespace/name information, owner element, resolved schema attribute data and name snapshot range.

These IXmlAttributeContext members are important:

Member Description

Element Property

Gets the IXmlElementContext upon which this attribute is located.

Name Property

Gets the name of the attribute.

NameSnapshotRange Property

Gets the TextSnapshotRange that specifies the range of the attribute name.

Namespace Property

Gets the namespace that defines the attribute.

NamespacePrefix Property

Gets the prefix of the namespace that defines the attribute.

QualifiedName Property

Gets the XmlQualifiedName that contains the attribute's qualified name.

SchemaAttribute Property

Gets the resolved XmlSchemaAttribute for this attribute, if known.