8

Title pretty much says it all. VS xsd.exe creates classes in camel case. Is there a way of generating the classes in pascal case?

If not does anyone know of a good tool to convert a load of class names from camel case to pascal case.

Thanks

EDIT:

xsd.exe doesn't generate class names in camel case as standard - it just follows the convention of the schema - however, I am interested in overriding this behaviour so the XmlElement name attribute will still follow the conventions defined by the schema, but the class name is pascal case.

jcvandan
  • 14,124
  • 18
  • 66
  • 103

2 Answers2

1

Check out XmlSchemaClassGenerator, an open source console application that includes PascalCasing among its features. (I found this after the 15-day trial of xsd2code expired.)

  • Map XML namespaces to C# namespaces, either explicitly or through a (configurable) function
  • Generate C# XML comments from schema annotations
  • Generate DataAnnotations attributes from schema restrictions
  • Use Collection<T> properties (initialized in constructor and with private setter)
  • Use either int, long, decimal, or string for xs:integer and derived types
  • Automatic properties
  • Pascal case for classes and properties
  • Generate nullable adapter properties for optional elements and attributes without default values (see below)
  • Optional support for PCL
  • Optional support for INotifyPropertyChanged

In my case, before I could get the console app to work, I had to modify the xsd document to include a targetNamespace field as follows:

Original:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified" version="1">

Modified:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="gateway" elementFormDefault="qualified" attributeFormDefault="unqualified" version="1">

My final usage looked something like this:

XmlSchemaClassGenerator.Console -n "http://www.w3.org/2001/XMLSchema=gateway" -o "C:\{output-folder}" {xsd-file-name}.xsd
Peter Majeed
  • 5,304
  • 2
  • 32
  • 57
1

I wasn't aware that xsd.exe did that, are you sure the types in the schema are not defined in camelCase also?

My suggestion would be to use xsd2code, which is far superior to xsd.exe in every way..

MattDavey
  • 8,897
  • 3
  • 31
  • 54
  • well the types in the schema are camel case - but is that standard xml practice? the majority of xml I see tends to be in camel case - therefore it would be nice if it automatically used pascal case, as is standard practice in .NET languages. is it possible to do what I asked in xsd2code? – jcvandan May 09 '11 at 15:04
  • it's not a standard practice as such but yes it is common (http://stackoverflow.com/questions/442529/is-there-a-standard-naming-convention-for-xml-elements). I don't think xsd2code will convert to PascalCase out of the box, which leaves you slightly short on options... first option would be to adopt PascalCase in your schemas. Second option would be to run an XSLT stylesheet against your schema which changes the names to PascalCase. Third option would be to do an automatic refactoring of the generated C# code. None of the options are particularly attractive! Sorry I couldn't help you more.. – MattDavey May 09 '11 at 15:37
  • Regarding the second option (XSLT), there is a function for converting identifiers to PascalCase - http://xsltsl.sourceforge.net/string.html#template.str:to-camelcase (note that in XSLT it's referred to as UpperCamelCase or TitleCase). Still a pain in the **** having to run the schemas through a stylesheet though :( – MattDavey May 09 '11 at 15:43
  • I cant change the schemas I'm afraid it's an external API I am interacting with - I may write a console app that does it automatically - or then again I will probably just leave it as it works :) thanks for the help anyway – jcvandan May 09 '11 at 16:00
  • I put in a feature suggestion on the xsd2code forum > http://xsd2code.codeplex.com/discussions/256920 – MattDavey May 10 '11 at 07:41
  • cool - it would be a nice feature - thx for the heads up with xsd2code it's a nice plugin – jcvandan May 10 '11 at 09:30