Bug with namespaces in XSD.EXE /dataset (C#)



I have come across an issue with XSD.exe code generation (in /dataset mode,
version 2.0.50727.1432), relating to namespaces. Specifically, certain types
are not always consistently prefixed with global::, so if you have a
namespace with the name System, the generated code will fail to compile.

The issue can occur specifically in two lines of the generated code, first
in the Add*Row function:

public SomeTableRow AddSomeTableRow(System.DateTime SomeDateTimeField) {

Which should be:

public SomeTableRow AddSomeTableRow(global::System.DateTime
SomeDateTimeField) {

And in the property get/set for any datetime field:

public System.DateTime SomeDateTimeField {

Which should be:

public global::System.DateTime SomeDateTimeField {

WORKAROUND

Don't use a namespace called System. We came across this because we are
generating database class libraries for a legacy system which included a
"system" 'namespace' in the data objects...

ISSUE REPRODUCTION

To reproduce this issue, take the following sample XSD:

<?xml version="1.0" standalone="yes"?>
<xs:schema id="DateTimeBug" xmlns=""
xmlns:xs="http://www.w3.org/2001/XMLSchema";
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="DateTimeBugDataSet" msdata:IsDataSet="true";
msdata:UseCurrentLocale="true"; msdata:EnforceConstraints="False";>
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="DateTimeBug">
<xs:complexType>
<xs:sequence>
<xs:element name="DateTimeField" type="xs:dateTime"
minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>

Generate a C# file with XSD:
xsd /dataset /n:MyCompany.Data.DateTimeBuggy datetimebug.xsd

Include the following namespace in second C# file:

namespace MyCompany.Data.System { };

Compile a project with these two C# files. It will fail with the error "The
type or namespace name 'DateTime' does not exist in the namespace
'MyCompany.Data.System' (are you missing an assembly reference?)"


.