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

后端 未结 3 1988
难免孤独
难免孤独 2020-12-05 13:47

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:

Namespa

3条回答
  •  星月不相逢
    2020-12-05 14:33

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

提交回复
热议问题