Using XSD with Microsoft Visual Studio

From Code Synthesis Wiki

(Difference between revisions)
Jump to: navigation, search
Revision as of 15:33, 3 February 2008
Boris (Talk | contribs)

← Previous diff
Revision as of 15:37, 3 February 2008
Boris (Talk | contribs)

Next diff →
Line 5: Line 5:
== Visual Studio .NET 2003 (7.1) == == Visual Studio .NET 2003 (7.1) ==
-A common way of integrating the XSD compilation step in Visual Studio (VS) 7.1 is with the custom build steps. With this method you add the schema file to your project and specify the command line to compile it as well as the output files. When the project is built, VS IDE will check if the output files do not exist or out-of-date compared to the schema file and will execute the specified command line in order to regenerate them. The following step-by-step instructions show how to acomplish this:+A common way of integrating the XSD compilation step in Visual Studio (VS) 7.1 is with the custom build steps. With this method you add the schema file to your project and specify the command line to compile it as well as the output files. When the project is built, VS IDE will check if the output files do not exist or out-of-date compared to the schema file and will execute the specified command line in order to regenerate them. The following step-by-step instructions show how to accomplish this:
# Add the schema file (for example, hello.xsd) to your project ("Project"->"Add Existing Item" menu action). At this point VS assumes that you are going to use Microsoft xsd.exe tool and will try to compile the schema which may fail. This is ok. You may also find that additional files (for example, hello.h) were added under the schema item. You can remove all of them. # Add the schema file (for example, hello.xsd) to your project ("Project"->"Add Existing Item" menu action). At this point VS assumes that you are going to use Microsoft xsd.exe tool and will try to compile the schema which may fail. This is ok. You may also find that additional files (for example, hello.h) were added under the schema item. You can remove all of them.
# Open the Properties dialog for the schema file (select the schema file in the Solution Explorer and choose "Project"->"Properties" menu action). # Open the Properties dialog for the schema file (select the schema file in the Solution Explorer and choose "Project"->"Properties" menu action).
# In the Properties dialog select the General tab and choose "Custom Build Tool" for the Tool field. Then press the Apply button. # In the Properties dialog select the General tab and choose "Custom Build Tool" for the Tool field. Then press the Apply button.
-# In the same Properties dialog select the "Custom Build Step" tab. There you will have the "Command Line", "Description", "Outputs", and "Additional Dependecies" fields. Fill them depending on how you want to compile your schema, for example:<br/> [[Image:Xsd-custom-build-step-vc71.jpg|Visual Studio 7.1 Custom Build Step dialog screenshot]]+# In the same Properties dialog select the "Custom Build Step" tab. There you will have the "Command Line", "Description", "Outputs", and "Additional Dependencies" fields. Fill them depending on how you want to compile your schema, for example:<br/> [[Image:Xsd-custom-build-step-vc71.jpg|Visual Studio 7.1 Custom Build Step dialog screenshot]]
# Press the Ok button to close the Properties dialog. # Press the Ok button to close the Properties dialog.
# Compile the schema by select the schema file in the Solution Explorer and choosing "Build"->"Compile" menu action. This will result in the output files (for example, hello.hxx and hello.cxx) being created. # Compile the schema by select the schema file in the Solution Explorer and choosing "Build"->"Compile" menu action. This will result in the output files (for example, hello.hxx and hello.cxx) being created.
# Add the output files (for example, hello.hxx and hello.cxx) to the project ("Project"->"Add Existing Item" menu action). # Add the output files (for example, hello.hxx and hello.cxx) to the project ("Project"->"Add Existing Item" menu action).
-# If your project is not linking to Xerces-C++ (or Expat, if you are using the C++/Parser mapping and selected Expat as the underlying XML parser), you should add xerces-c_2.lib (release) or xerces-c_2D.lib (debug) (libexpat.lib for Expat) as Additional Dependecies in the Linker's Input sub-tab. To accomplish this, select your project in Solution Explorer and choose "Project"->"Properties" menu action. Select the Linker tab, then Input sub-tab, and enter the library name into the Additional Dependecies field.+# If your project is not linking to Xerces-C++ (or Expat, if you are using the C++/Parser mapping and selected Expat as the underlying XML parser), you should add xerces-c_2.lib (release) or xerces-c_2D.lib (debug) (libexpat.lib for Expat) as Additional Dependencies in the Linker's Input sub-tab. To accomplish this, select your project in Solution Explorer and choose "Project"->"Properties" menu action. Select the Linker tab, then Input sub-tab, and enter the library name into the Additional Dependencies field.
== Visual Studio 2005 (8.0) and 2008 (9.0) == == Visual Studio 2005 (8.0) and 2008 (9.0) ==
-For Visual Studio 2005 and 2008 there are two ways to setup the XSD compilation step. You can use the same custom build step approach as in Visual Studio 2003. Additionally, starting from Visual Studio 2005, you can use the rules-based approach of setting up the XSD compilation. This approach relies on the .rules files that are provided with the XSD distributions. These files specify the command line, output files, and options for the XSD compiler. They also provide the GUI-based presentation which allows you to configure the schema compilation process in way similar to the built-in tools such as the C++ compiler and linker. The following step-by-step instructions show how to acomplish this:+For Visual Studio 2005 and 2008 there are two ways to setup the XSD compilation step. You can use the same custom build step approach as in Visual Studio 2003. Additionally, starting from Visual Studio 2005, you can use the rules-based approach of setting up the XSD compilation. This approach relies on the .rules files that are provided with the XSD distributions. These files specify the command line, output files, and options for the XSD compiler. They also provide the GUI-based presentation which allows you to configure the schema compilation process in way similar to the built-in tools such as the C++ compiler and linker. The following step-by-step instructions show how to accomplish this:
# The .rules files are located in the etc\vc-8.0 and etc\vc-9.0 directories (for VS 2005 and 2008, respectively) in the XSD distribution. The first step is to add one of these directories to the list of paths where Visual Studio looks for .rules files. To accomplish this, open the Options dialog by choosing the "Tools"->"Options" menu action. Then in the "Projects and Solutions" tab select the "VC++ Project Settings" sub-tab. Add the path to either etc\vc-8.0 (for VS 2005) or etc\vc-9.0 (for VS 2008) to the "Rule File Search Paths" field. Then press the Ok button to close this dialog. # The .rules files are located in the etc\vc-8.0 and etc\vc-9.0 directories (for VS 2005 and 2008, respectively) in the XSD distribution. The first step is to add one of these directories to the list of paths where Visual Studio looks for .rules files. To accomplish this, open the Options dialog by choosing the "Tools"->"Options" menu action. Then in the "Projects and Solutions" tab select the "VC++ Project Settings" sub-tab. Add the path to either etc\vc-8.0 (for VS 2005) or etc\vc-9.0 (for VS 2008) to the "Rule File Search Paths" field. Then press the Ok button to close this dialog.
# Select the project where you want to add your schema and open the "Custom Build Rules" dialog by choosing "Project"->"Custom Build Rules" menu action. There your should see two rules for CodeSynthesis XSD: one for C++/Tree and the other is for C++/Parser. Select the rule corresponding to the mapping you are planning to use in your project. Press the Ok button to close the dialog. For example:<br/> [[Image:Xsd-custom-build-rules-vc80.jpg|Visual Studio 8.0 Custom Build Rules dialog screenshot]] # Select the project where you want to add your schema and open the "Custom Build Rules" dialog by choosing "Project"->"Custom Build Rules" menu action. There your should see two rules for CodeSynthesis XSD: one for C++/Tree and the other is for C++/Parser. Select the rule corresponding to the mapping you are planning to use in your project. Press the Ok button to close the dialog. For example:<br/> [[Image:Xsd-custom-build-rules-vc80.jpg|Visual Studio 8.0 Custom Build Rules dialog screenshot]]
# Add the schema file (for example, hello.xsd) to your project ("Project"->"Add Existing Item" menu action). At this point VS may add additional files (for example, hello.h) under the schema item. You can remove all of them. # Add the schema file (for example, hello.xsd) to your project ("Project"->"Add Existing Item" menu action). At this point VS may add additional files (for example, hello.h) under the schema item. You can remove all of them.
-# Open the Properties dialog for the schema file (select the schema file in the Solution Explorer and choose "Project"->"Properties" menu action). There you will notice that the Tool field in the General type contains the name of the C++ mapping that you have choosen.+# Open the Properties dialog for the schema file (select the schema file in the Solution Explorer and choose "Project"->"Properties" menu action). There you will notice that the Tool field in the General type contains the name of the C++ mapping that you have chosen.
# In the same Properties dialog select the "C++/Tree Mapping Rule" or "C++/Parser Mapping Rule" tab. There you will see a number of sub-tabs where you can select various XSD compiler options, for example:<br/> [[Image:Xsd-cxx-tree-build-rule-vc80.jpg|Visual Studio 8.0 C++/Tree mapping rule screenshot]] # In the same Properties dialog select the "C++/Tree Mapping Rule" or "C++/Parser Mapping Rule" tab. There you will see a number of sub-tabs where you can select various XSD compiler options, for example:<br/> [[Image:Xsd-cxx-tree-build-rule-vc80.jpg|Visual Studio 8.0 C++/Tree mapping rule screenshot]]
# Press the Ok button to close the Properties dialog. # Press the Ok button to close the Properties dialog.
# Compile the schema by select the schema file in the Solution Explorer and choosing "Build"->"Compile" menu action. This will result in the output files (for example, hello.hxx and hello.cxx) being created. # Compile the schema by select the schema file in the Solution Explorer and choosing "Build"->"Compile" menu action. This will result in the output files (for example, hello.hxx and hello.cxx) being created.
# Add the output files (for example, hello.hxx and hello.cxx) to the project ("Project"->"Add Existing Item" menu action). # Add the output files (for example, hello.hxx and hello.cxx) to the project ("Project"->"Add Existing Item" menu action).
-# If your project is not linking to Xerces-C++ (or Expat, if you are using the C++/Parser mapping and selected Expat as the underlying XML parser), you should add xerces-c_2.lib (release) or xerces-c_2D.lib (debug) (libexpat.lib for Expat) as Additional Dependecies in the Linker's Input sub-tab. To accomplish this, select your project in Solution Explorer and choose "Project"->"Properties" menu action. Select the Linker tab, then Input sub-tab, and enter the library name into the Additional Dependecies field.+# If your project is not linking to Xerces-C++ (or Expat, if you are using the C++/Parser mapping and selected Expat as the underlying XML parser), you should add xerces-c_2.lib (release) or xerces-c_2D.lib (debug) (libexpat.lib for Expat) as Additional Dependencies in the Linker's Input sub-tab. To accomplish this, select your project in Solution Explorer and choose "Project"->"Properties" menu action. Select the Linker tab, then Input sub-tab, and enter the library name into the Additional Dependencies field.
== Creating a DLL == == Creating a DLL ==
-This section explains how to create a dynamically-linked library (DLL) from the generated code. It assumes that you are using the .rules file approach as presented in the previous section. You can also achive the same result by using the custom build step method.+This section explains how to create a dynamically-linked library (DLL) from the generated code. It assumes that you are using the .rules file approach as presented in the previous section. You can also achieve the same result by using the custom build step method.
1. Create a DLL project, let's call it foo, and add the schema to it as explained in the previous section. 1. Create a DLL project, let's call it foo, and add the schema to it as explained in the previous section.

Revision as of 15:37, 3 February 2008

Note: this page is a work in progress and is based on the upcoming XSD 3.1.0

This page describes various ways of integrating the XSD compiler with the Microsoft Visual Studio IDE as well as other Visual Studio-specific topics.

Visual Studio .NET 2003 (7.1)

A common way of integrating the XSD compilation step in Visual Studio (VS) 7.1 is with the custom build steps. With this method you add the schema file to your project and specify the command line to compile it as well as the output files. When the project is built, VS IDE will check if the output files do not exist or out-of-date compared to the schema file and will execute the specified command line in order to regenerate them. The following step-by-step instructions show how to accomplish this:

  1. Add the schema file (for example, hello.xsd) to your project ("Project"->"Add Existing Item" menu action). At this point VS assumes that you are going to use Microsoft xsd.exe tool and will try to compile the schema which may fail. This is ok. You may also find that additional files (for example, hello.h) were added under the schema item. You can remove all of them.
  2. Open the Properties dialog for the schema file (select the schema file in the Solution Explorer and choose "Project"->"Properties" menu action).
  3. In the Properties dialog select the General tab and choose "Custom Build Tool" for the Tool field. Then press the Apply button.
  4. In the same Properties dialog select the "Custom Build Step" tab. There you will have the "Command Line", "Description", "Outputs", and "Additional Dependencies" fields. Fill them depending on how you want to compile your schema, for example:
    Visual Studio 7.1 Custom Build Step dialog screenshot
  5. Press the Ok button to close the Properties dialog.
  6. Compile the schema by select the schema file in the Solution Explorer and choosing "Build"->"Compile" menu action. This will result in the output files (for example, hello.hxx and hello.cxx) being created.
  7. Add the output files (for example, hello.hxx and hello.cxx) to the project ("Project"->"Add Existing Item" menu action).
  8. If your project is not linking to Xerces-C++ (or Expat, if you are using the C++/Parser mapping and selected Expat as the underlying XML parser), you should add xerces-c_2.lib (release) or xerces-c_2D.lib (debug) (libexpat.lib for Expat) as Additional Dependencies in the Linker's Input sub-tab. To accomplish this, select your project in Solution Explorer and choose "Project"->"Properties" menu action. Select the Linker tab, then Input sub-tab, and enter the library name into the Additional Dependencies field.

Visual Studio 2005 (8.0) and 2008 (9.0)

For Visual Studio 2005 and 2008 there are two ways to setup the XSD compilation step. You can use the same custom build step approach as in Visual Studio 2003. Additionally, starting from Visual Studio 2005, you can use the rules-based approach of setting up the XSD compilation. This approach relies on the .rules files that are provided with the XSD distributions. These files specify the command line, output files, and options for the XSD compiler. They also provide the GUI-based presentation which allows you to configure the schema compilation process in way similar to the built-in tools such as the C++ compiler and linker. The following step-by-step instructions show how to accomplish this:

  1. The .rules files are located in the etc\vc-8.0 and etc\vc-9.0 directories (for VS 2005 and 2008, respectively) in the XSD distribution. The first step is to add one of these directories to the list of paths where Visual Studio looks for .rules files. To accomplish this, open the Options dialog by choosing the "Tools"->"Options" menu action. Then in the "Projects and Solutions" tab select the "VC++ Project Settings" sub-tab. Add the path to either etc\vc-8.0 (for VS 2005) or etc\vc-9.0 (for VS 2008) to the "Rule File Search Paths" field. Then press the Ok button to close this dialog.
  2. Select the project where you want to add your schema and open the "Custom Build Rules" dialog by choosing "Project"->"Custom Build Rules" menu action. There your should see two rules for CodeSynthesis XSD: one for C++/Tree and the other is for C++/Parser. Select the rule corresponding to the mapping you are planning to use in your project. Press the Ok button to close the dialog. For example:
    Visual Studio 8.0 Custom Build Rules dialog screenshot
  3. Add the schema file (for example, hello.xsd) to your project ("Project"->"Add Existing Item" menu action). At this point VS may add additional files (for example, hello.h) under the schema item. You can remove all of them.
  4. Open the Properties dialog for the schema file (select the schema file in the Solution Explorer and choose "Project"->"Properties" menu action). There you will notice that the Tool field in the General type contains the name of the C++ mapping that you have chosen.
  5. In the same Properties dialog select the "C++/Tree Mapping Rule" or "C++/Parser Mapping Rule" tab. There you will see a number of sub-tabs where you can select various XSD compiler options, for example:
    Visual Studio 8.0 C++/Tree mapping rule screenshot
  6. Press the Ok button to close the Properties dialog.
  7. Compile the schema by select the schema file in the Solution Explorer and choosing "Build"->"Compile" menu action. This will result in the output files (for example, hello.hxx and hello.cxx) being created.
  8. Add the output files (for example, hello.hxx and hello.cxx) to the project ("Project"->"Add Existing Item" menu action).
  9. If your project is not linking to Xerces-C++ (or Expat, if you are using the C++/Parser mapping and selected Expat as the underlying XML parser), you should add xerces-c_2.lib (release) or xerces-c_2D.lib (debug) (libexpat.lib for Expat) as Additional Dependencies in the Linker's Input sub-tab. To accomplish this, select your project in Solution Explorer and choose "Project"->"Properties" menu action. Select the Linker tab, then Input sub-tab, and enter the library name into the Additional Dependencies field.

Creating a DLL

This section explains how to create a dynamically-linked library (DLL) from the generated code. It assumes that you are using the .rules file approach as presented in the previous section. You can also achieve the same result by using the custom build step method.

1. Create a DLL project, let's call it foo, and add the schema to it as explained in the previous section.

2. Create a header file, let's call it export.hxx, which contains the export symbol definitions:

#ifdef FOO_EXPORTS
#define FOO_SYMBOL_DECL __declspec(dllexport)
#else
#define FOO_SYMBOL_DECL __declspec(dllimport)
#endif

3. Open the Properties dialog for the schema file, select the Code Generation tab and enter FOO_SYMBOL_DECL into the Export Symbol field. Then select the Command Line tab and add --hxx-prologue "#include \"export.hxx\"" to the Additional Options field.

4. Rebuild the project.

Personal tools