26
votes

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?

3

3 Answers

45
votes

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
3
votes

Citation from here:

   XmlSchema guidSchema = new XmlSchema();
   guidSchema.TargetNamespace = "http://microsoft.com/wsdl/types/";

   XmlSchemaSimpleTypeRestriction guidRestriction = new XmlSchemaSimpleTypeRestriction();
   guidRestriction.BaseTypeName = new XmlQualifiedName("string", XmlSchema.Namespace);

   XmlSchemaPatternFacet guidPattern = new XmlSchemaPatternFacet();
   guidPattern.Value = @"[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}";
   guidRestriction.Facets.Add(guidPattern);

   XmlSchemaSimpleType guidType = new XmlSchemaSimpleType();
   guidType.Name = "guid";
   guidType.Content = guidRestriction;
   guidSchema.Items.Add(guidType);

   schemaSet.Add(guidSchema);

   XmlSchema speakerSchema = new XmlSchema();
   speakerSchema.TargetNamespace = "http://www.microsoft.com/events/teched2005/";

   // ...

   XmlSchemaElement idElement = new XmlSchemaElement();
   idElement.Name = "ID";

   // Here's where the magic happens...

   idElement.SchemaTypeName = new XmlQualifiedName("guid", "http://microsoft.com/wsdl/types/");
1
votes

It looks like that wsdl namespace extension page was deleted, so it can't find the type information you need.