public static XmlSchemaSet GetXmlSchemaSet(ICollection<Type> operationTypes) { var exporter = new XsdDataContractExporter(); exporter.Export(operationTypes); exporter.Schemas.Compile(); return exporter.Schemas; }
public static XmlSchemaSet GetXmlSchemaSet(ICollection<Type> operationTypes) { var exporter = new XsdDataContractExporter(); var types = operationTypes .Where(x => !x.AllAttributes<ExcludeAttribute>() .Any(attr => attr.Feature.HasFlag(Feature.Soap))).ToList(); exporter.Export(types); exporter.Schemas.Compile(); return exporter.Schemas; }
static void Main(string[] args) { XsdDataContractExporter xsdexp = new XsdDataContractExporter(); xsdexp.Options = new ExportOptions(); xsdexp.Export(typeof(Employee)); // Write out exported schema to a file using (FileStream fs = new FileStream("sample.xsd", FileMode.Create)) foreach (XmlSchema sch in xsdexp.Schemas.Schemas()) sch.Write(fs); }
public void PrimitiveType () { XsdDataContractExporter xdce = new XsdDataContractExporter (); Assert.AreEqual (1, xdce.Schemas.Count); Assert.IsNull (xdce.GetSchemaType (typeof (int))); Assert.AreEqual (new QName ("int", XmlSchema.Namespace), xdce.GetSchemaTypeName (typeof (int))); xdce.Export (typeof (int)); Assert.IsNull (xdce.GetSchemaType (typeof (int))); Assert.AreEqual (new QName ("int", XmlSchema.Namespace), xdce.GetSchemaTypeName (typeof (int))); }
public void ExportXSDUsingDataContractExporter() { //create schema XsdDataContractExporter _exporter = new XsdDataContractExporter(); Type _ConfigurationDataType = typeof(ConfigurationData); Assert.IsTrue(_exporter.CanExport(_ConfigurationDataType)); _exporter.Export(_ConfigurationDataType); Console.WriteLine("number of schemas: {0}", _exporter.Schemas.Count); Console.WriteLine(); //write out the schema XmlSchemaSet _Schemas = _exporter.Schemas; XmlQualifiedName XmlNameValue = _exporter.GetRootElementName(_ConfigurationDataType); string EmployeeNameSpace = XmlNameValue.Namespace; foreach (XmlSchema _schema in _Schemas.Schemas(EmployeeNameSpace)) _schema.Write(Console.Out); }
public static string GetXsd(Type operationType) { if (operationType == null) return null; var sb = new StringBuilder(); var exporter = new XsdDataContractExporter(); if (exporter.CanExport(operationType)) { exporter.Export(operationType); var mySchemas = exporter.Schemas; var qualifiedName = exporter.GetRootElementName(operationType); if (qualifiedName == null) return null; foreach (XmlSchema schema in mySchemas.Schemas(qualifiedName.Namespace)) { schema.Write(new StringWriter(sb)); } } return sb.ToString(); }
public static string GetSchema(IEnumerable<Type> typesToExport) { XsdDataContractExporter exporter = new XsdDataContractExporter(); Type headType = null; foreach (Type t in typesToExport) { if(headType ==null) headType = t; if (!exporter.CanExport(t)) throw new ArgumentException("cannot export type: " + t.ToString()); exporter.Export(t); Console.WriteLine("number of schemas: {0}", exporter.Schemas.Count); Console.WriteLine(); } XmlSchemaSet schemas = exporter.Schemas; XmlQualifiedName XmlNameValue = exporter.GetRootElementName(headType); string ns = XmlNameValue.Namespace; StringWriter w = new StringWriter(); foreach (XmlSchema schema in schemas.Schemas(ns)) { //if(schema. schema.Write(w); } Debug.WriteLine(w.ToString()); return schemas.ToString(); }
Message CreateExample(Type type, OperationDescription od, bool generateJson) { bool usesXmlSerializer = od.Behaviors.Contains(typeof(XmlSerializerOperationBehavior)); XmlQualifiedName name; XmlSchemaSet schemaSet = new XmlSchemaSet(); IDictionary<XmlQualifiedName, Type> knownTypes = new Dictionary<XmlQualifiedName, Type>(); if (usesXmlSerializer) { XmlReflectionImporter importer = new XmlReflectionImporter(); XmlTypeMapping typeMapping = importer.ImportTypeMapping(type); name = new XmlQualifiedName(typeMapping.ElementName, typeMapping.Namespace); XmlSchemas schemas = new XmlSchemas(); XmlSchemaExporter exporter = new XmlSchemaExporter(schemas); exporter.ExportTypeMapping(typeMapping); foreach (XmlSchema schema in schemas) { schemaSet.Add(schema); } } else { XsdDataContractExporter exporter = new XsdDataContractExporter(); List<Type> listTypes = new List<Type>(od.KnownTypes); listTypes.Add(type); exporter.Export(listTypes); if (!exporter.CanExport(type)) { throw new NotSupportedException(String.Format("Example generation is not supported for type '{0}'", type)); } name = exporter.GetRootElementName(type); foreach (Type knownType in od.KnownTypes) { XmlQualifiedName knownTypeName = exporter.GetSchemaTypeName(knownType); if (!knownTypes.ContainsKey(knownTypeName)) { knownTypes.Add(knownTypeName, knownType); } } foreach (XmlSchema schema in exporter.Schemas.Schemas()) { schemaSet.Add(schema); } } schemaSet.Compile(); XmlWriterSettings settings = new XmlWriterSettings { CloseOutput = false, Indent = true, }; if (generateJson) { var jsonExample = new XDocument(); using (XmlWriter writer = XmlWriter.Create(jsonExample.CreateWriter(), settings)) { HelpExampleGenerator.GenerateJsonSample(schemaSet, name, writer, knownTypes); } var reader = jsonExample.CreateReader(); reader.MoveToContent(); var message = Message.CreateMessage(MessageVersion.None, (string)null, reader); WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain"; message.Properties[WebBodyFormatMessageProperty.Name] = new WebBodyFormatMessageProperty(WebContentFormat.Json); return message; } else { var xmlExample = new XDocument(); using (XmlWriter writer = XmlWriter.Create(xmlExample.CreateWriter(), settings)) { HelpExampleGenerator.GenerateXmlSample(schemaSet, name, writer); } var reader = xmlExample.CreateReader(); reader.MoveToContent(); var message = Message.CreateMessage(MessageVersion.None, (string)null, reader); message.Properties[WebBodyFormatMessageProperty.Name] = new WebBodyFormatMessageProperty(WebContentFormat.Xml); WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml"; return message; } }
public void Test2 () { XsdDataContractExporter xdce = new XsdDataContractExporter (); xdce.Export (typeof (dc)); XmlSchemaSet set = xdce.Schemas; xdce = new XsdDataContractExporter (set); try { xdce.Export (typeof (dc)); } catch (XmlSchemaException xe) { return; } catch (Exception e) { Assert.Fail ("Expected XmlSchemaException, but got " + e.GetType ().ToString ()); } Assert.Fail ("Expected XmlSchemaException"); }
// If the user schema is generated by WCF, it may contain references to some primitive WCF // types in the following namespaces. We need add those schemas to the schema set by default // so these types can be resolved correctly. // // * http://schemas.microsoft.com/2003/10/Serialization // * http://schemas.microsoft.com/2003/10/Serialization/Arrays // * http://microsoft.com/wsdl/types/ // * http://schemas.datacontract.org/2004/07/System private void AddSchemasForPrimitiveTypes(XmlSchemaSet schemas) { // Add DCS special types XsdDataContractExporter dataContractExporter = new XsdDataContractExporter(schemas); // We want to export Guid, Char, and TimeSpan, however even a single call causes all // schemas to be exported. dataContractExporter.Export(typeof(Guid)); // Export DateTimeOffset, DBNull, array types dataContractExporter.Export(typeof(DateTimeOffset)); dataContractExporter.Export(typeof(DBNull)); dataContractExporter.Export(typeof(bool[])); dataContractExporter.Export(typeof(DateTime[])); dataContractExporter.Export(typeof(decimal[])); dataContractExporter.Export(typeof(double[])); dataContractExporter.Export(typeof(float[])); dataContractExporter.Export(typeof(int[])); dataContractExporter.Export(typeof(long[])); dataContractExporter.Export(typeof(XmlQualifiedName[])); dataContractExporter.Export(typeof(short[])); dataContractExporter.Export(typeof(string[])); dataContractExporter.Export(typeof(uint[])); dataContractExporter.Export(typeof(ulong[])); dataContractExporter.Export(typeof(ushort[])); dataContractExporter.Export(typeof(Char[])); dataContractExporter.Export(typeof(TimeSpan[])); dataContractExporter.Export(typeof(Guid[])); // Arrays of DateTimeOffset and DBNull are not supported // Add XS special types // XmlSchemaExporter takes XmlSchemas so we need that temporarily XmlSchemas xmlSchemas = new XmlSchemas(); XmlReflectionImporter importer = new XmlReflectionImporter(); XmlSchemaExporter xmlExporter = new XmlSchemaExporter(xmlSchemas); xmlExporter.ExportTypeMapping(importer.ImportTypeMapping(typeof(Guid))); xmlExporter.ExportTypeMapping(importer.ImportTypeMapping(typeof(Char))); foreach (XmlSchema schema in xmlSchemas) { schemas.Add(schema); } }
public void Dc3Test2 () { //Check for duplicate dc2 ? XsdDataContractExporter xdce = new XsdDataContractExporter (); xdce.Export (typeof (dc3)); xdce.Export (typeof (dc3)); CheckDcFull (xdce.Schemas); }
internal MessageHelpInformation(OperationDescription od, bool isRequest, Type type, bool wrapped) { this.Type = type; this.SupportsJson = WebHttpBehavior.SupportsJsonFormat(od); string direction = isRequest ? SR2.GetString(SR2.HelpPageRequest) : SR2.GetString(SR2.HelpPageResponse); if (wrapped && !typeof(void).Equals(type)) { this.BodyDescription = SR2.GetString(SR2.HelpPageBodyIsWrapped, direction); this.FormatString = SR2.GetString(SR2.HelpPageUnknown); } else if (typeof(void).Equals(type)) { this.BodyDescription = SR2.GetString(SR2.HelpPageBodyIsEmpty, direction); this.FormatString = SR2.GetString(SR2.HelpPageNA); } else if (typeof(Message).IsAssignableFrom(type)) { this.BodyDescription = SR2.GetString(SR2.HelpPageIsMessage, direction); this.FormatString = SR2.GetString(SR2.HelpPageUnknown); } else if (typeof(Stream).IsAssignableFrom(type)) { this.BodyDescription = SR2.GetString(SR2.HelpPageIsStream, direction); this.FormatString = SR2.GetString(SR2.HelpPageUnknown); } else if (typeof(Atom10FeedFormatter).IsAssignableFrom(type)) { this.BodyDescription = SR2.GetString(SR2.HelpPageIsAtom10Feed, direction); this.FormatString = WebMessageFormat.Xml.ToString(); } else if (typeof(Atom10ItemFormatter).IsAssignableFrom(type)) { this.BodyDescription = SR2.GetString(SR2.HelpPageIsAtom10Entry, direction); this.FormatString = WebMessageFormat.Xml.ToString(); } else if (typeof(AtomPub10ServiceDocumentFormatter).IsAssignableFrom(type)) { this.BodyDescription = SR2.GetString(SR2.HelpPageIsAtomPubServiceDocument, direction); this.FormatString = WebMessageFormat.Xml.ToString(); } else if (typeof(AtomPub10CategoriesDocumentFormatter).IsAssignableFrom(type)) { this.BodyDescription = SR2.GetString(SR2.HelpPageIsAtomPubCategoriesDocument, direction); this.FormatString = WebMessageFormat.Xml.ToString(); } else if (typeof(Rss20FeedFormatter).IsAssignableFrom(type)) { this.BodyDescription = SR2.GetString(SR2.HelpPageIsRSS20Feed, direction); this.FormatString = WebMessageFormat.Xml.ToString(); } else if (typeof(SyndicationFeedFormatter).IsAssignableFrom(type)) { this.BodyDescription = SR2.GetString(SR2.HelpPageIsSyndication, direction); this.FormatString = WebMessageFormat.Xml.ToString(); } else if (typeof(XElement).IsAssignableFrom(type) || typeof(XmlElement).IsAssignableFrom(type)) { this.BodyDescription = SR2.GetString(SR2.HelpPageIsXML, direction); this.FormatString = WebMessageFormat.Xml.ToString(); } else { try { bool usesXmlSerializer = od.Behaviors.Contains(typeof(XmlSerializerOperationBehavior)); XmlQualifiedName name; this.SchemaSet = new XmlSchemaSet(); IDictionary<XmlQualifiedName, Type> knownTypes = new Dictionary<XmlQualifiedName, Type>(); if (usesXmlSerializer) { XmlReflectionImporter importer = new XmlReflectionImporter(); XmlTypeMapping typeMapping = importer.ImportTypeMapping(this.Type); name = new XmlQualifiedName(typeMapping.ElementName, typeMapping.Namespace); XmlSchemas schemas = new XmlSchemas(); XmlSchemaExporter exporter = new XmlSchemaExporter(schemas); exporter.ExportTypeMapping(typeMapping); foreach (XmlSchema schema in schemas) { this.SchemaSet.Add(schema); } } else { XsdDataContractExporter exporter = new XsdDataContractExporter(); List<Type> listTypes = new List<Type>(od.KnownTypes); bool isQueryable; Type dataContractType = DataContractSerializerOperationFormatter.GetSubstituteDataContractType(this.Type, out isQueryable); listTypes.Add(dataContractType); exporter.Export(listTypes); if (!exporter.CanExport(dataContractType)) { this.BodyDescription = SR2.GetString(SR2.HelpPageCouldNotGenerateSchema); this.FormatString = SR2.GetString(SR2.HelpPageUnknown); return; } name = exporter.GetRootElementName(dataContractType); DataContract typeDataContract = DataContract.GetDataContract(dataContractType); if (typeDataContract.KnownDataContracts != null) { foreach (XmlQualifiedName dataContractName in typeDataContract.KnownDataContracts.Keys) { knownTypes.Add(dataContractName, typeDataContract.KnownDataContracts[dataContractName].UnderlyingType); } } foreach (Type knownType in od.KnownTypes) { XmlQualifiedName knownTypeName = exporter.GetSchemaTypeName(knownType); if (!knownTypes.ContainsKey(knownTypeName)) { knownTypes.Add(knownTypeName, knownType); } } foreach (XmlSchema schema in exporter.Schemas.Schemas()) { this.SchemaSet.Add(schema); } } this.SchemaSet.Compile(); XmlWriterSettings settings = new XmlWriterSettings { CloseOutput = false, Indent = true, }; if (this.SupportsJson) { XDocument exampleDocument = new XDocument(); using (XmlWriter writer = XmlWriter.Create(exampleDocument.CreateWriter(), settings)) { HelpExampleGenerator.GenerateJsonSample(this.SchemaSet, name, writer, knownTypes); } this.JsonExample = exampleDocument.Root; } if (name.Namespace != "http://schemas.microsoft.com/2003/10/Serialization/") { foreach (XmlSchema schema in this.SchemaSet.Schemas(name.Namespace)) { this.Schema = schema; } } XDocument XmlExampleDocument = new XDocument(); using (XmlWriter writer = XmlWriter.Create(XmlExampleDocument.CreateWriter(), settings)) { HelpExampleGenerator.GenerateXmlSample(this.SchemaSet, name, writer); } this.XmlExample = XmlExampleDocument.Root; } catch (Exception e) { if (Fx.IsFatal(e)) { throw; } this.BodyDescription = SR2.GetString(SR2.HelpPageCouldNotGenerateSchema); this.FormatString = SR2.GetString(SR2.HelpPageUnknown); this.Schema = null; this.JsonExample = null; this.XmlExample = null; } } }
public void EnumNoDcTest () { XsdDataContractExporter xdce = new XsdDataContractExporter (); xdce.Export (typeof (EnumNoDc)); CheckEnum (xdce.Schemas, new QName ("EnumNoDc", "http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"), new List<string> (new string [] { "Red", "Green", "Blue" })); }
public void DcTest () { XsdDataContractExporter xdce = new XsdDataContractExporter (); xdce.Export (typeof (dc)); CheckDcFull (xdce.Schemas); }
public void EnumTest () { XsdDataContractExporter xdce = new XsdDataContractExporter (); xdce.Export (typeof (XColors)); CheckEnum (xdce.Schemas, colors_qname, new List<string> (new string [] { "_Red" })); }
/// <summary> /// Returns the sequence of properties of the specified type. /// </summary> /// <param name="type">The type.</param> /// <param name="schema">The schema.</param> /// <param name="schemaSet">The schema set.</param> /// <param name="serializationManager">The serialization manager.</param> /// <returns>Sequence containing all properties.</returns> private static XmlSchemaSequence GetPropertiesSequence(Type type, XmlSchema schema, XmlSchemaSet schemaSet, ISerializationManager serializationManager) { Argument.IsNotNull("type", type); Argument.IsNotNull("schema", schema); Argument.IsNotNull("schemaSet", schemaSet); var propertiesSequence = new XmlSchemaSequence(); if (typeof(ModelBase).IsAssignableFromEx(type)) { var members = new List<MemberInfo>(); members.AddRange(from field in serializationManager.GetFieldsToSerialize(type) select type.GetFieldEx(field)); members.AddRange(from property in serializationManager.GetPropertiesToSerialize(type) select type.GetPropertyEx(property)); foreach (var member in members) { var propertySchemaElement = new XmlSchemaElement(); propertySchemaElement.Name = member.Name; var memberType = typeof(object); var fieldInfo = member as FieldInfo; if (fieldInfo != null) { memberType = fieldInfo.FieldType; } var propertyInfo = member as PropertyInfo; if (propertyInfo != null) { memberType = propertyInfo.PropertyType; } propertySchemaElement.IsNillable = memberType.IsNullableType(); propertySchemaElement.MinOccurs = 0; var exporter = new XsdDataContractExporter(schemaSet); exporter.Export(memberType); propertySchemaElement.SchemaType = exporter.GetSchemaType(memberType); propertySchemaElement.SchemaTypeName = exporter.GetSchemaTypeName(memberType); propertiesSequence.Items.Add(propertySchemaElement); } } return propertiesSequence; }
Message CreateSchema(Type body, bool isXmlSerializerType) { System.Collections.IEnumerable schemas; if (isXmlSerializerType) { XmlReflectionImporter importer = new XmlReflectionImporter(); XmlTypeMapping typeMapping = importer.ImportTypeMapping(body); XmlSchemas s = new XmlSchemas(); XmlSchemaExporter exporter = new XmlSchemaExporter(s); exporter.ExportTypeMapping(typeMapping); schemas = s.GetSchemas(null); } else { XsdDataContractExporter exporter = new XsdDataContractExporter(); exporter.Export(body); schemas = exporter.Schemas.Schemas(); } using (MemoryStream stream = new MemoryStream()) { XmlWriterSettings xws = new XmlWriterSettings() { Indent = true }; using (XmlWriter w = XmlWriter.Create(stream, xws)) { w.WriteStartElement("Schemas"); foreach (XmlSchema schema in schemas) { if (schema.TargetNamespace != "http://www.w3.org/2001/XMLSchema") { schema.Write(w); } } } stream.Seek(0, SeekOrigin.Begin); using (XmlReader reader = XmlReader.Create(stream)) { return Message.CreateMessage(MessageVersion.None, null, XElement.Load(reader, LoadOptions.PreserveWhitespace)); } } }
/// <summary> /// Returns the sequence of properties of the specified type. /// </summary> /// <param name="type">The type.</param> /// <param name="schema">The schema.</param> /// <param name="schemaSet">The schema set.</param> /// <param name="serializationManager">The serialization manager.</param> /// <param name="exportedTypes">The exported types.</param> /// <returns>Sequence containing all properties.</returns> private static XmlSchemaSequence GetPropertiesSequence(Type type, XmlSchema schema, XmlSchemaSet schemaSet, ISerializationManager serializationManager, HashSet<string> exportedTypes) { Argument.IsNotNull("type", type); Argument.IsNotNull("schema", schema); Argument.IsNotNull("schemaSet", schemaSet); var propertiesSequence = new XmlSchemaSequence(); if (type.IsModelBase()) { var members = new List<MemberMetadata>(); members.AddRange(from field in serializationManager.GetFieldsToSerialize(type) select field.Value); members.AddRange(from property in serializationManager.GetCatelPropertiesToSerialize(type) select property.Value); members.AddRange(from property in serializationManager.GetRegularPropertiesToSerialize(type) select property.Value); foreach (var member in members) { var propertySchemaElement = new XmlSchemaElement(); propertySchemaElement.Name = member.MemberName; var memberType = member.MemberType; propertySchemaElement.IsNillable = memberType.IsNullableType(); propertySchemaElement.MinOccurs = 0; var exporter = new XsdDataContractExporter(schemaSet); var alreadyExported = IsAlreadyExported(schemaSet, memberType, exporter, exportedTypes); if (!alreadyExported) { if (!exportedTypes.Contains(memberType.FullName)) { exportedTypes.Add(memberType.FullName); } try { if (exporter.CanExport(memberType)) { exporter.Export(memberType); } } catch (Exception) { // Ignore } } propertySchemaElement.SchemaType = exporter.GetSchemaType(memberType); propertySchemaElement.SchemaTypeName = exporter.GetSchemaTypeName(memberType); propertiesSequence.Items.Add(propertySchemaElement); } } return propertiesSequence; }
internal static List<WebServiceTypeData> GetKnownTypes(Type type, WebServiceTypeData typeData) { List<WebServiceTypeData> knownTypes = new List<WebServiceTypeData>(); XsdDataContractExporter exporter = new XsdDataContractExporter(); exporter.Export(type); ICollection schemas = exporter.Schemas.Schemas(); foreach (XmlSchema schema in schemas) { // DataContractSerializer always exports built-in types into a fixed schema that can be ignored. if (schema.TargetNamespace == SerializationNamespace) { continue; } foreach (XmlSchemaObject schemaObj in schema.Items) { XmlSchemaType schemaType = schemaObj as XmlSchemaType; string schemaTargetNamespace = XmlConvert.DecodeName(schema.TargetNamespace); if (schemaType != null && !(schemaType.Name == typeData.TypeName && schemaTargetNamespace == typeData.TypeNamespace) && !String.IsNullOrEmpty(schemaType.Name)) { WebServiceTypeData knownTypeData = null; XmlSchemaSimpleTypeRestriction simpleTypeRestriction; if (CheckIfEnum(schemaType as XmlSchemaSimpleType, out simpleTypeRestriction)) { knownTypeData = ImportEnum(XmlConvert.DecodeName(schemaType.Name), schemaTargetNamespace, schemaType.QualifiedName, simpleTypeRestriction, schemaType.Annotation); } else if (CheckIfCollection(schemaType as XmlSchemaComplexType)) { continue; } else if (!(schemaType is XmlSchemaSimpleType)) { knownTypeData = new WebServiceTypeData(XmlConvert.DecodeName(schemaType.Name), schemaTargetNamespace); } if (knownTypeData != null) { knownTypes.Add(knownTypeData); } } } } return knownTypes; }