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

删除回忆录丶 提交于 2019-11-27 20:29:22
erbi

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
boj

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/");

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!