Enable IntelliSense in Your Documents

June 25, 2008 at 2:41 pm Leave a comment

Visual Studio has provided an XML editor that offers IntelliSense for any given XML schema since Visual Studio 2003. Microsoft upgraded this XML editor in Visual Studio 2005.In VS 2003, IntelliSense works if the namespace declaration URI (Uniform Resource Identifier) matches one of the schemas in your local VS folder. This has always worked well and continues to work in VS 2005, but it’s actually the wrong approach to validate XML schemas.

I’ll cover the limitations of the VS 2003 approach for providing IntelliSense in your XML documents, including why you shouldn’t use namespace URIs for schema validation. I will also cover a better approach, using the XML-Schema Instance (XSI) namespace and how you can use it to solve the problems presented by the original implementation. Finally, I will show you how to create an example that leverages the new XML editor in VS 2005, using the XSI namespace to perform validation and provide IntelliSense against any locally or remotely hosted XML schemas.

There are many reasons why you might want to edit XML documents in Visual Studio. For example, you might want to use it to edit a Web.Config file, an XSL Transformation (XSLT), or any XML input/configuration files for your own custom applications. To enable XML IntelliSense in Visual Studio 2003, locate your schema, then copy it to C:\Program Files\Microsoft Visual Studio .NET 2003\Common7\Packages\schemas\xml. Next, open a new XML document, and reference the schema by its namespace URI in the root element.

By default, VS 2003 provides a File/New XSLT template, but doesn’t include the schema. You can download the freely available schema from Fesersoft. By copying the schema to the location mentioned above, you will start to get IntelliSense. Updating the File/New xsltfile.xslt template is optional. It is located in C:\Program Files\Microsoft Visual Studio .NET 2003\Common7\IDE\NewFileItems.

This has been a good solution for providing IntelliSense for XML documents, and my own company has been using it for more than two years. It’s effective, but has a couple weaknesses.

The first is basically an inconvenience; the schemas must reside locally. This creates a problem when XML schemas need to be distributed to a team of developers, especially if the schemas are evolving and/or are not centrally located. You can overcome this problem by scripting Visual Studio’s OnStartupComplete event to download the schemas from multiple locations to the local drive.

The second problem with this technique relates to a fundamental XML issue that occurs when you use XML namespace URIs to locate schemas for validation and IntelliSense. The purpose of XML namespaces is for qualifying elements and attributes, similar to how people qualify a first name with a last name. The syntax of a namespace declaration is xmlns:prefix=”anyURI” where you can express the URI as either a Uniform Resource Locator (URL) or a Uniform Resource Name (URN). In other words, you can write a namespace URI as either http://www.foo.com or urn:www-foo-com.

What’s in a Name(space)?
It is mere coincidence that many namespace URIs look like valid URLs. In fact, if you navigate to XSLT’s namespace declaration URL (http://www.w3.org/1999/XSL/Transform), you’ll get a schema that reads: “Someday a schema for XSL Transformations will live here.” A namespace URI is nothing more than a label, and technically, it provides no information about the location of the corresponding schema. This explains why the schema files must reside locally in VS 2003. There is no other way to locate them.

The problem remains: You need a way to associate an XML document’s namespace URIs to their schema locations. You might be aware that the XML-Schema Instance (XSI) namespace is used to define types, such as xsi:type=”integer.” One of the more powerful and lesser known uses of this namespace is its ability to associate XML schemas.

Let’s examine the name of the XML-Schema Instance namespace. In object-oriented programming, you have classes and objects that represent the static compile time and dynamic runtime parts of an application. Objects are running instances of classes. XML behaves similarly; a schema document is like a class. When you create a new XML document that uses a schema, it’s like creating an object instance of a class. This new XML document is an instance document. The name “XML-Schema Instance” implies that there is a separation of a schema and its instance. It also implies that you will use the schema within an instance document.

Conversely, namespace declarations can be used in both schema documents and instance documents. Namespace declarations provide uniquely named labels, but the XSI namespace provides the ability to associate an instance document with its schema(s) and to reference the schema’s types (see Table 1).

The XSI namespace has two attributes for associating schemas: xsi:noNamespaceSchemaLocation and xsi:schemaLocation. You can use xsi:noNamespaceSchemaLocation to reference your schema if the schema does not define a target namespace:

<foo xmlns:xsi="?"
xsi:noNamespaceSchemaLocation=
"http://www.foo.com/foo.xsd"
>

Advertisements

Entry filed under: c-sharp. Tags: , , , .

sharepoint 2003 info ASP.NET DataGrid

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


June 2008
M T W T F S S
« Apr   Jul »
 1
2345678
9101112131415
16171819202122
23242526272829
30  

%d bloggers like this: