Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pascal case class names when generating code with Visual Studio xsd.exe

Tags:

c#

.net

xsd.exe

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.

like image 627
jcvandan Avatar asked May 09 '11 13:05

jcvandan


People also ask

What is XSD exe?

The XML Schema Definition (Xsd.exe) tool generates XML schema or common language runtime classes from XDR, XML, and XSD files, or from classes in a runtime assembly.

What is the use of XSD file in C#?

XSD can easily define parent-child relationship and basic type restrictions needed to define tables - so can be used as database schema. "MSDataSetGenerator" is the "custom tool" that builds whatever files/binaries are needed from the XSD whenever XSD is saved.


2 Answers

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..

like image 195
MattDavey Avatar answered Oct 24 '22 03:10

MattDavey


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
like image 42
Peter Majeed Avatar answered Oct 24 '22 01:10

Peter Majeed