What is the correct way of using the Guid type in a XSD file?

erbi picture erbi · Mar 27, 2009 · Viewed 31.7k times · Source

I have a .xsd file which I use to generate code with the xsd.exe tool from Visual Studio. Some class members are Guids and the xsd.exe tool gives 2 warnings:

Namespace 'http://microsoft.com/wsdl/types/' is not available to be referenced in this schema. Type 'http://microsoft.com/wsdl/types/:guid' is not declared.

The Guid type is recognized because the generated C# file is valid and works. Anyone knows how to get rid of those warnings?

What is the correct syntax for the XSD to be validated AND class members being generated as System.Guid?

Answer

erbi picture erbi · Apr 6, 2009

Thank you all, I found how to remove the warnings.

As sysrqb said, the wsdl namespace has either been deleted or never existed. It seems that the xsd.exe tool knows the Guid definition internally, but it cannot validate the xsd schema.

As boj pointed out, the only way to validate the schema with Guids in it, is to (re)define that type in a schema. The trick here is to add the Guid type to the same "http://microsoft.com/wsdl/types" namespace. This way, the xsd.exe will do the proper association between http://microsoft.com/wsdl/types:Guid and System.Guid

I made a new xsd file for the guid type:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://microsoft.com/wsdl/types/" >
    <xs:simpleType name="guid">
        <xs:annotation>
            <xs:documentation xml:lang="en">
                The representation of a GUID, generally the id of an element.
            </xs:documentation>
        </xs:annotation>
        <xs:restriction base="xs:string">
            <xs:pattern value="\{[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}\}"/>
        </xs:restriction>
    </xs:simpleType>
</xs:schema>

Then, I run xsd.exe with both my original xsd file and this new xsd file:

xsd.exe myschema.xsd guid.xsd /c