From 7b0ce5193f99c330c9453fe6ccb5e619ace162f0 Mon Sep 17 00:00:00 2001 From: Michael Ganss Date: Thu, 22 Sep 2016 15:23:49 +0200 Subject: [PATCH] Add support for fixed values (fixes #16) Create models for global types before those for global elements (fixes #17) --- .../xsd/client/client.xsd | 2 +- XmlSchemaClassGenerator/Generator.cs | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/XmlSchemaClassGenerator.Tests/xsd/client/client.xsd b/XmlSchemaClassGenerator.Tests/xsd/client/client.xsd index 2b6daab8..ed530d0b 100644 --- a/XmlSchemaClassGenerator.Tests/xsd/client/client.xsd +++ b/XmlSchemaClassGenerator.Tests/xsd/client/client.xsd @@ -65,7 +65,7 @@ - + diff --git a/XmlSchemaClassGenerator/Generator.cs b/XmlSchemaClassGenerator/Generator.cs index 284fb1ba..39c52bf5 100644 --- a/XmlSchemaClassGenerator/Generator.cs +++ b/XmlSchemaClassGenerator/Generator.cs @@ -337,6 +337,13 @@ private void BuildModel() AttributeGroups = Set.Schemas().Cast().SelectMany(s => s.AttributeGroups.Values.Cast()).ToDictionary(g => g.QualifiedName); Groups = Set.Schemas().Cast().SelectMany(s => s.Groups.Values.Cast()).ToDictionary(g => g.QualifiedName); + foreach (var globalType in Set.GlobalTypes.Values.Cast()) + { + var schema = globalType.GetSchema(); + var source = (schema == null ? null : new Uri(schema.SourceUri)); + var type = CreateTypeModel(source, globalType, globalType.QualifiedName); + } + foreach (var rootElement in Set.GlobalElements.Values.Cast()) { var source = new Uri(rootElement.GetSchema().SourceUri); @@ -388,13 +395,6 @@ private void BuildModel() type.RootElementName = rootElement.QualifiedName; } } - - foreach (var globalType in Set.GlobalTypes.Values.Cast()) - { - var schema = globalType.GetSchema(); - var source = (schema == null ? null : new Uri(schema.SourceUri)); - var type = CreateTypeModel(source, globalType, globalType.QualifiedName); - } } // see http://msdn.microsoft.com/en-us/library/z2w0sxhf.aspx @@ -707,7 +707,7 @@ private IEnumerable CreatePropertiesForAttributes(Uri source, Typ Type = CreateTypeModel(source, attribute.AttributeSchemaType, attributeQualifiedName), IsAttribute = true, IsNullable = attribute.Use != XmlSchemaUse.Required, - DefaultValue = attribute.DefaultValue, + DefaultValue = attribute.DefaultValue ?? (attribute.Use != XmlSchemaUse.Optional ? attribute.FixedValue : null), Form = attribute.Form == XmlSchemaForm.None ? attribute.GetSchema().AttributeFormDefault : attribute.Form, XmlNamespace = attribute.QualifiedName.Namespace != "" && attribute.QualifiedName.Namespace != typeModel.XmlSchemaName.Namespace ? attribute.QualifiedName.Namespace : null, }; @@ -779,7 +779,7 @@ private IEnumerable CreatePropertiesForElements(Uri source, TypeM IsNillable = element.IsNillable, IsNullable = item.MinOccurs < 1.0m, IsCollection = item.MaxOccurs > 1.0m || particle.MaxOccurs > 1.0m, // http://msdn.microsoft.com/en-us/library/vstudio/d3hx2s7e(v=vs.100).aspx - DefaultValue = element.DefaultValue, + DefaultValue = element.DefaultValue ?? (item.MinOccurs >= 1.0m ? element.FixedValue : null), Form = element.Form == XmlSchemaForm.None ? element.GetSchema().ElementFormDefault : element.Form, XmlNamespace = element.QualifiedName.Namespace != "" && element.QualifiedName.Namespace != typeModel.XmlSchemaName.Namespace ? element.QualifiedName.Namespace : null, };