Schema based intellisence to custom files (with extension other than .xml)
At times we have some files with custom extensions, like I had to use an extension “.casl”, but I wanted to incorporate the intellisence based on a XSD schema file to that casl file. Just like we do attach a schema to xml files and we get the intellisence available in the xml file. To achieve this we need to do the following.
1. Locate the VS.net installation directory (e.g I have VS.net 2008 installed at C:\program files\Microsoft Visual Studio 9.0)
2. You would be able to see a folder named “xml” and inside that folder you will see a folder named “schema” (on my system I can browse to the following path to get inside the schema folder C:\program files\Microsoft Visual Studio 9.0\Xml\Schemas)
3. Inside “schema” folder you will find a configuration file named “catalog.xml”
4. Open “catalog.xml” in the VS.net and add the following association tag (<Association extension=“casl“ schema=“c:/CaslSchema.xsd“ condition=“*“>) just save this file and open the file with casl extension and you will be able to use the intellisence feature
Please note that you must place the custom schema at the path you specify inside the schema attribute (schema=“c:/CaslSchema.xsd“) in the above example I have placed it at C:/
To achieve this programmatically please try the following code
Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@”Software\Microsoft\VisualStudio\9.0″, true);
string installationDirectoryPath = rk.GetValue(“InstallDir”).ToString();
installationDirectoryPath = installationDirectoryPath.Replace(“\\Common7\\IDE\\”, “”);
string configurationFilePath = installationDirectoryPath + “\\Xml\\Schemas\\catalog.xml”;
System.IO.StreamReader sr = new StreamReader(new FileStream(configurationFilePath,System.IO.FileMode.Open,System.IO.FileAccess.ReadWrite));
string fileContents = sr.ReadToEnd();
sr.Close();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(fileContents);
XmlNode node = xmlDoc.LastChild;
bool associationExisits = false;
foreach (XmlNode childNode in node.ChildNodes)
{
if (childNode.Attributes["extension"] != null)
{
if (childNode.Attributes["extension"].Value.ToLower() == “casl”)
{
associationExisits = true;
break;
}
}
}
if (!associationExisits)
{
XmlNode lastAssociationChild = node.LastChild;
XmlNode newNode = lastAssociationChild.Clone();
newNode.Attributes["extension"].Value = “casl”;
newNode.Attributes["schema"].Value = @”C:\CamlSchema.xsd”;
newNode.Attributes["condition"].Value = “*”;
node.AppendChild(newNode);
xmlDoc.LoadXml(“<SchemaCatalog xmlns=\”http://schemas.microsoft.com/xsd/catalog\”>” + node.InnerXml + “</SchemaCatalog>”);
StreamWriter writer = new StreamWriter(new FileStream(configurationFilePath, System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite));
writer.Flush();
writer.Write(xmlDoc.InnerXml);
writer.Close();
}
if there is any issue in this, please feel free to contact me at irfanyar@gmail.com
Cheers
Irfan Yar