Create a Single File Generator that uses an XML source file to generate a new C# file in Visual Studio.
- Technologies: Visual Studio 2015 SDK
- Topics: Visual Studio Shell, MSDBuild, VSX
Description
A single file generator pulls information from a source file and generates a new file from the data. This sample single file generator uses an XML Schema to pull data from an XML file and generate a C# file. The generator leverages CodeDom to generate C# source code. It also demonstrates how to validate an XML document against a schema and communicate errors through the Error List.
Requirements
Get all samples
Clone the repo (How to):
git clone https://github.com/Microsoft/VSSDK-Extensibility-Samples.git
Run the sample
- To run the sample, hit F5 or choose the Debug > Start Debugging menu command. A new experimental instance of Visual Studio will launch.
- Once loaded, create a new project. Ex:_ WpfApplication_
- Add a new XML file to the project. File > Add > New File
- Populate the new XML file with content using the schema from XmlClassGeneratorSchema.xsd. For a working example, Copy/Paste the code from Example.XmlFile.xml into your new XML file.
- Open the Properties window for the xml file and set CustomTool to XmlClassGenerator.
- Save the XML file. This triggers the single file generator to run and generate a new C# file.
- The new C# file is added to the project with the same name as the XML file. This new file appears in Solution Explorer as a dependent of the original XML file node.
- Any errors will be reported in the Error List toolwindow.
Project Files
- AssemblyInfo.cs
This file contains assembly custom attributes.
- BaseCodeGenerator.cs
Abstract class that implements the IVsSingleFileGenerator interface.
- BaseCodeGeneratorWithSite.cs
Abstract class that inherits from BaseCodeGenerator and implements the IObjectWithSite interface.
- XmlClassGenerator.cs
The single file generator class.
- SourceCodeGenerator.cs
Static class that contains the CodeDom code used to generate source code.
- XmlClassGeneratorSchema.xsd
XML Schema document tells the generator how to convert source code.
- Strings.resx
Resource strings (localizable).
Functional Tests
- Verify the sample builds in Debug Configuration
- Verify the sample builds in Release Configuration
- Verify that the generator is properly registered when building
- Verify that the generator works
- Verify that the generator fails for bad XML
** Related topics **