private ClrPropertyInfo BuildProperty(XmlSchemaAttribute attribute, bool fromBaseType, bool isNew, ClrTypeInfo containingType = null) { string schemaName = attribute.QualifiedName.Name; string schemaNs = attribute.QualifiedName.Namespace; string propertyName = localSymbolTable.AddAttribute(attribute); Occurs attrPropertyOccurs = attribute.Use == XmlSchemaUse.Required ? Occurs.One : Occurs.ZeroOrOne; ClrPropertyInfo propertyInfo = new ClrPropertyInfo(propertyName, schemaNs, schemaName, attrPropertyOccurs); propertyInfo.Origin = SchemaOrigin.Attribute; propertyInfo.FromBaseType = fromBaseType; propertyInfo.IsNew = isNew; propertyInfo.VerifyRequired = configSettings.VerifyRequired; XmlSchemaSimpleType schemaType = attribute.AttributeSchemaType; var isInlineEnum = attribute.AttributeSchemaType.IsEnum() && attribute.AttributeSchemaType.IsDerivedByRestriction() && ((attribute.AttributeSchemaType.Content as XmlSchemaSimpleTypeRestriction)?.Facets .Cast <XmlSchemaObject>().Any() ?? false); var isAnonymous = !attribute.AttributeSchemaType.IsGlobal() && !attribute.AttributeSchemaType.IsBuiltInSimpleType(); var qName = schemaType.QualifiedName; if (qName.IsEmpty) { qName = attribute.QualifiedName; } // http://linqtoxsd.codeplex.com/WorkItem/View.aspx?WorkItemId=4106 ClrTypeReference typeRef = BuildTypeReference(schemaType, qName, isAnonymous, true); if (isInlineEnum && isAnonymous) { typeRef.Name += "Enum"; if (typeRef.ClrFullTypeName.IsNullOrEmpty()) { var typeScopedResolutionString = containingType?.GetNestedTypeScopedResolutionString(); if (typeScopedResolutionString.IsNullOrEmpty()) // if this is empty, then take the referencing element { var closestNamedParent = attribute.GetClosestNamedParent().GetPotentialName(); typeScopedResolutionString = closestNamedParent; } typeRef.UpdateClrFullTypeName(propertyInfo, typeScopedResolutionString); } } propertyInfo.TypeReference = typeRef; Debug.Assert(schemaType.Datatype != null); SetFixedDefaultValue(attribute, propertyInfo); return(propertyInfo); }
public void TestGetClosestNamedParent1() { Assert.IsTrue(File.Exists(xsdFilePathPubmed)); var xsd = XmlSchema.Read(File.OpenText(xsdFilePathPubmed), (sender, args) => { }); var urlElement = xsd.Items.Cast <XmlSchemaObject>() .OfType <XmlSchemaElement>() .First(e => e.Name == "URL"); XmlSchemaAttribute langAttr = null; if (urlElement.SchemaType is XmlSchemaComplexType ctx) { if (ctx.ContentModel is XmlSchemaSimpleContent xssc) { if (xssc.Content is XmlSchemaSimpleContentExtension xssce) { var attrs = xssce.Attributes.Cast <XmlSchemaAttribute>().ToList(); if (attrs.Any()) { langAttr = attrs.First(a => a.Name == "lang"); } } } } Assert.IsNotNull(langAttr); var namedParent = langAttr.GetClosestNamedParent(); Assert.IsNotNull(namedParent); var parentElement = namedParent as XmlSchemaElement; Assert.IsTrue(parentElement != null); Assert.IsTrue(parentElement.Name == "URL"); }