How to Integrate Your Own XML Document Types into VS.NET Projects

I. Enable Intellisense Support

1. Create XML Schema

Create a XML Schema for the XML files you want to edit within VS.NET. (If you´re not familiar with XML Schema, start out with an example XML file and use xsd.exe (one of the VS.NET tools) to generate a XML Schema.)

Let´s assume your XML Schema is called myschema.xsd. It should start out like this:

<xs:schema id="MyXMLFiletype" 
                  targetNamespace="http://schemas.mydomain.com/MySchema"
                  xmlns="http://schemas.mydomain.com/MySchema"
                  xmlns:xs="http://www.w3.org/2001/XMLSchema">
...

Be sure to include an id and a targetNamespace! The targetNamespace will be shown later in VS.NET in a list to select from.


2. Register File Type

Come up with a file type for your XML files, e.g. .xyz. What you now want is, that VS.NET knows, .xyz files are XML files and supports you with the XML editor when working on such files.

Create a registry .reg file to make the .xyz file type know to VS.NET and be associated with the XML editor. Copy the following to a text file you save as xyz.reg. Replace all xyz occurrences with your file extension:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\7.1\Editors\{8281C572-2171-45AA-A642-7D8BC1662F1C}\Extensions]
"xyz"=dword:00000027

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\7.1\Editors\{C76D83F8-A489-11D0-8195-00A0C91BBEE3}\Extensions]
"xyz"=dword:00000028

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\7.1\Languages\File Extensions\.xyz]
@="{58E975A0-F8FE-11D2-A6AE-00104BCC7269}"
"unused"="HTML"

After you executed the xyz.reg file, whenever you open a .xyz file in VS.NET it´s displayed with colored elements and attributes.


3. Register XML Schema with VS.NET

Copy the XML Schema to the following directory:

C:\Program Files\Microsoft Visual Studio .NET 2003\Common7\Packages\schemas\xml

From then on, when you open an .xyz file, create the appropriate root element and type the attribute name xmlns= on it, a list of schemas pops up and you can select http://schemas.mydomain.com/MySchema to be associated with the file.

As soon as this schema is referenced in your .xyz file, you have intellisens and VS.NET will point out any validation errors.


II. Add New File Type

If you want to be able to create a new file of type .xyz from within VS.NET, you need to register a new file item.

1. Create File Item Template

Create a template file for new .xyz files, e.g. by creating a file which just contains the root element and the xmlns= attribute with the namespace. Call the file myxyz.xyz.


2. Register File Item Template

First copy myxyz.xyz to C:\Program Files\Microsoft Visual Studio .NET 2003\Common7\IDE\NewFileItems.

Then make it know to VS.NET by adding a line to the file NewFileItems.vsdir like the following:

myxyz.xyz|{DA9FB551-C724-11d0-AE1F-00A0C90FFFC3}|XYZ File|110|Create cool XYZ files with a snap|0|#6815|0|myxyz.xyz

From then on you´ll see a new item for you .xyz files in the list of menu item File|New|File of VS.NET.

Files you create this way, though, don´t belong to a project. If you want to easily include .xyz file into your VB.NET or C# projects, you need to register a new item type with VS.NET.


III. Add New Item Type

If you want to add .xyz files to your projects, you need to set up a new item type. Following are the steps to register a new item type for VB.NET projects. For C# projects it´s equally easy; just replace references to the VB-language with ones for C# in directory names etc.

1. Create Item Wizard Launch File

Create a VS.NET wizard launch file xyz.vsz like this:

VSWIZARD 7.0
Wizard=VsWizard.VsWizardEngine.7.1
Param="WIZARD_NAME = XYZFile"
Param="WIZARD_UI = FALSE"
Param="PROJECT_TYPE = VBPROJ"

Place xyz.vsz in C:\Program Files\Microsoft Visual Studio .NET 2003\Vb7\VBProjectItems.


2. Register Wizard Launch File

In C:\Program Files\Microsoft Visual Studio .NET 2003\Vb7\VBProjectItems\Local Project Items open LocalProjectItems.vsdir and add a line like this to register the above wizard launch file:

..\xyz.vsz|{164B10B9-B200-11D0-8C61-00A0C91E29D5}|XYZ File|75|Create cool XYZ files in your projects|{164B10B9-B200-11D0-8C61-00A0C91E29D5}|4539| |myxyz.xyz


3. Create Item Wizard

3.1 Copy the directory C:\Program Files\Microsoft Visual Studio .NET 2003\Vb7\VBWizards\XMLFile to

C:\Program Files\Microsoft Visual Studio .NET 2003\Vb7\VBWizards\XYZFile

where XYZFile is the value of WIZARD_NAME in the above launch file.

3.2 Copy into

C:\Program Files\Microsoft Visual Studio .NET 2003\Vb7\VBWizards\XYZFile\Templates\1033

the template file you already used up in step II.1.

3.3. Open default.js in C:\Program Files\Microsoft Visual Studio .NET 2003\Vb7\VBWizards\SqlObjectsFile\Scripts\1033.

Replace XMLFile.Xml therein with the name of your template file, myxyz.xyz.

1 Comment

  • @Stefan: You&#180;re right! I overlooked the &quot;Set as Default&quot;-Button on the &quot;Open with...&quot; dialog.



    After you clicked &quot;Set as Default&quot;, the next time you create a project item with the same extension the XML Editor is associated with it.



    Thanks for your tip!

Comments have been disabled for this content.