As I mentioned yesterday, you can write a custom SchemaImporterExtension component to customize the output from XSD. Basically I want to write my own extension so that I can control some of the syntax and semantics that are produced by XSD when I run it to create my business entity classes. For example, out-of-the box XSD will name my fields something like: "rolecodeField" and doesn't create nice, strongly typed collections the way I want. All-in-all I just want a very generic SchemaImporterExtension that will give me something like:
Fields: _camelCase
Properties: PascalCase
Collections: CustomCollection Inherits List<T>
At this stage I really have no idea how to write such a component but I have worked out how to attach the debugger to XSD to debug the class as you are writing it...
Grab XSD.exe (C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin) and copy it into the build directory for your project - most likely the \Bin\Debug folder. Next, create an XSD.exe.config for XSD and add the following configuration information to it, this is the bit that tells XSD.exe to hook to your SchemaImporterExtension.
<configuration>
<system.xml.serialization>
<schemaImporterExtensions>
<!-- This should refer to your SchemaImporterExtention class -->
<add
name="MarkItUp.GenericSchemaImporterExtention"
type="MarkItUp.GenericSchemaImporterExtention, Common, Version=1.0.0.0"
/>
</schemaImporterExtensions>
</system.xml.serialization>
</configuration>
Next, open up the Project Properties window for your Project and open up the "Debug" tab. Set the start action to "Start External Application" and point it to the XSD.exe program that you copied into your \bin folder. Something like:
C:\Projects\SchemaImporterTests\Common\bin\Debug\xsd.exe
In the start options field, type in the command line arguments that will allow XSD.exe to point to a .xsd file. This will look something like:
"C:\Projects\SchemaImporterTests\XSD\Test.xsd" /classes /o:"C:\Projects\SchemaImporterTests\XSD\Bin"
This will create classes based on Test.xsd and output a file named Test.cs into the C:\Projects\SchemaImporterTests\XSD\Bin folder.
Finally, create your GenericSchemaImporterExtention class in your project and set a breakpoint somewhere in the ImportSchemaType method:
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization.Advanced;
namespace Common
{
class GenericSchemaImporterExtention : SchemaImporterExtension
{
public override string ImportSchemaType(...)
{
string foo = "bar";
return foo;
}
}
}
When you press Debug/Start your debug symbol will be hit! Now you simply need to add the few small lines of code into the ImportSchemaType method and you are done, easy huh? :-)