private XmlSchemaGroupBase AddElement(Action <XmlSchemaElement> add, string name, bool required, bool multiple, XmlSchemaComplexType type = null) { var element = new XmlSchemaElement(); if (!required) { element.MinOccurs = 0; element.MaxOccursString = multiple ? "unbounded" : "1"; } else if (multiple) { element.MinOccurs = 1; element.MaxOccursString = "unbounded"; } XmlSchemaComplexType currentType = type == null ? new XmlSchemaComplexType() : type; var all = new XmlSchemaAll(); element.Name = name; if (type == null) { element.SchemaType = currentType; } else { element.SchemaTypeName = new XmlQualifiedName(currentType.Name); } currentType.Particle = all; add(element); return(all); }
private void GenerateParticleAllField(XmlSchemaAll xsall) { foreach (XmlSchemaParticle cp in xsall.Items) { GenerateParticleField(cp); } }
private static void OutputElements(ComplexType padre, XmlSchemaParticle particle) { XmlSchemaAll all = particle as XmlSchemaAll; if (all == null) { return; } foreach (XmlSchemaObject xmlSchemaObject in all.Items) { XmlSchemaElement childElement = xmlSchemaObject as XmlSchemaElement; if (childElement == null) { continue; } padre.Attributes.Add(new Attribute { AttributeName = childElement.Name, IsMandatory = childElement.MinOccurs > 0, AttributeType = childElement.SchemaTypeName.Name }); } }
protected virtual void Visit(XmlSchemaAll particle) { if (particle.MaxOccurs > 0) { Traverse(particle.Items); } }
/// <summary> /// Чтение содержимого внутри элемента /// </summary> /// <param name="childElement">Считываемый элемент</param> private void ReadXSDParticle(XmlSchemaElement childElement) { if (childElement.ElementSchemaType is XmlSchemaComplexType) { XmlSchemaComplexType complexType = childElement.ElementSchemaType as XmlSchemaComplexType; if (complexType.Particle is XmlSchemaAll) { XmlSchemaAll all = complexType.Particle as XmlSchemaAll; // <xsd:all> Properties.MinOccursAll = all.MinOccurs.ToString(); foreach (XmlSchemaElement childElementInAll in all.Items) { ReadXSD(childElementInAll); } } if (complexType.Particle is XmlSchemaSequence) { XmlSchemaSequence seq = complexType.Particle as XmlSchemaSequence; // <xsd:sequence> foreach (XmlSchemaObject childElementInSeq in seq.Items) { ReadXSD(childElementInSeq); } } } }
public override XmlSchemaObject VisitASTType(ASTType astType) { var t = new XmlSchemaComplexType { Name = astType.Name }; var fields = astType.Fields.Select(Visit); var all = new XmlSchemaAll(); foreach (var field in fields) { all.Items.Add(field); } t.Particle = all; var description = string.Join(" ", astType.Annotations.Select(a => a.Value)); var schemaAnnotation = new XmlSchemaAnnotation(); var docs = new XmlSchemaDocumentation() { Markup = TextToNodeArray(description) }; schemaAnnotation.Items.Add(docs); t.Annotation = schemaAnnotation; this.Schema.Items.Add(t); ExtractElement(astType); return(t); }
private void GenerateAll(XmlSchemaAll all, InstanceGroup grp) { XmlSchemaParticle pt; for (int i = all.Items.Count; i > 0; i--) { pt = (XmlSchemaParticle)(all.Items[i - 1]); GenerateParticle(pt, false, grp); } }
private XmlSchemaGroupBase AddType(XmlSchema schema, string typeName) { var type = new XmlSchemaComplexType(); type.Name = typeName; var all = new XmlSchemaAll(); schema.Items.Add(type); type.Particle = all; return(all); }
protected override void Visit(XmlSchemaAll particle) { if (particle.MaxOccurs == 0) { return; } PushNode(ChildType.All, particle.Parent, particle); base.Visit(particle); PopNode(); }
IEnumerable <XElement> CreateProtoAll(XmlSchemaAll all) { foreach (XmlSchemaParticle particle in all.Items) { if (((int)particle.MinOccurs) > 0) { foreach (var val in CreateProtoParticle(particle).Repeat((int)particle.MinOccurs)) { yield return(val); } } } }
/// <summary> /// Creates a schema from attributes in the given types /// </summary> /// <param name="CategoryToFields">Lookup for all field settings</param> /// <returns>New schema instance</returns> static XmlSchema CreateSchema(Dictionary <string, Dictionary <string, FieldInfo> > CategoryToFields) { // Create elements for all the categories XmlSchemaAll RootAll = new XmlSchemaAll(); foreach (KeyValuePair <string, Dictionary <string, FieldInfo> > CategoryPair in CategoryToFields) { string CategoryName = CategoryPair.Key; XmlSchemaAll CategoryAll = new XmlSchemaAll(); foreach (KeyValuePair <string, FieldInfo> FieldPair in CategoryPair.Value) { XmlSchemaElement Element = CreateSchemaFieldElement(FieldPair.Key, FieldPair.Value.FieldType); CategoryAll.Items.Add(Element); } XmlSchemaComplexType CategoryType = new XmlSchemaComplexType(); CategoryType.Particle = CategoryAll; XmlSchemaElement CategoryElement = new XmlSchemaElement(); CategoryElement.Name = CategoryName; CategoryElement.SchemaType = CategoryType; CategoryElement.MinOccurs = 0; CategoryElement.MaxOccurs = 1; RootAll.Items.Add(CategoryElement); } // Create the root element and schema object XmlSchemaComplexType RootType = new XmlSchemaComplexType(); RootType.Particle = RootAll; XmlSchemaElement RootElement = new XmlSchemaElement(); RootElement.Name = XmlConfigFile.RootElementName; RootElement.SchemaType = RootType; XmlSchema Schema = new XmlSchema(); Schema.TargetNamespace = XmlConfigFile.SchemaNamespaceURI; Schema.ElementFormDefault = XmlSchemaForm.Qualified; Schema.Items.Add(RootElement); // Finally compile it XmlSchemaSet SchemaSet = new XmlSchemaSet(); SchemaSet.Add(Schema); SchemaSet.Compile(); return(SchemaSet.Schemas().OfType <XmlSchema>().First()); }
/// <summary>Determines whether the specified <paramref name="element"/> is expected.</summary> /// <param name="element">Element to check.</param> /// <param name="elementName"> /// <see cref="XmlQualifiedName"/> of the <paramref name="element"/> (passed to avoid recreation). /// </param> /// <param name="expected">Expected schema particle.</param> /// <returns>true if the element is expected; false otherwise.</returns> private static bool IsElementExpected(XElement element, XmlQualifiedName elementName, XmlSchemaParticle expected) { Debug.Assert(element != null, "element != null"); Debug.Assert(elementName != null, "elementName != null"); Debug.Assert(expected != null, "expected != null"); Debug.Assert( ToQualifiedName(element.Name) == elementName, "ToQualifiedName(element.Name) == elementName -- otherwise the caller get the 'caching' wrong"); // These are all possibilities for a particle. XmlSchemaGroupRef schemaGroupRef = expected as XmlSchemaGroupRef; XmlSchemaAny schemaAny = expected as XmlSchemaAny; XmlSchemaElement schemaElement = expected as XmlSchemaElement; XmlSchemaAll schemaAll = expected as XmlSchemaAll; XmlSchemaChoice schemaChoice = expected as XmlSchemaChoice; XmlSchemaSequence schemaSequence = expected as XmlSchemaSequence; Debug.Assert(schemaGroupRef == null, "schemaGroupRef == null -- the validator flattens this out as options."); Debug.Assert(schemaSequence == null, "schemaSequence == null -- the validator flattens this out and picks the right one in seq."); Debug.Assert(schemaAll == null, "schemaAll == null -- the validator flattens this out as options."); Debug.Assert(schemaChoice == null, "schemaChoice == null -- the validator flattens this out as options."); if (schemaAny != null) { Debug.Assert( schemaAny.Namespace == "##other" || schemaAny.Namespace == "##any", "schemaAny.Namespace == '##other' || '##any' -- otherwise CSDL XSD resource was changed"); if (schemaAny.Namespace == "##any") { return(true); } else if (schemaAny.Namespace == "##other") { string realElementNamespace = element.Name.NamespaceName; if (realElementNamespace != GetTargetNamespace(expected)) { return(true); } } } if (schemaElement != null) { if (schemaElement.QualifiedName == elementName) { return(true); } } return(false); }
private void Write43_XmlSchemaAll(XmlSchemaAll o) { if (o != null) { this.WriteStartElement("all"); this.WriteAttribute("id", "", o.Id); this.WriteAttribute("minOccurs", "", XmlConvert.ToString(o.MinOccurs)); this.WriteAttribute("maxOccurs", "", (o.MaxOccurs == 79228162514264337593543950335M) ? "unbounded" : XmlConvert.ToString(o.MaxOccurs)); this.WriteAttributes(o.UnhandledAttributes, o); this.Write5_XmlSchemaAnnotation(o.Annotation); this.WriteSortedItems(o.Items); this.WriteEndElement(); } }
void Write43_XmlSchemaAll(XmlSchemaAll o) { if ((object)o == null) { return; } WriteStartElement("all"); WriteAttribute(@"id", @"", ((System.String)o.@Id)); WriteAttribute("minOccurs", "", XmlConvert.ToString(o.MinOccurs)); WriteAttribute("maxOccurs", "", o.MaxOccurs == decimal.MaxValue ? "unbounded" : XmlConvert.ToString(o.MaxOccurs)); WriteAttributes((XmlAttribute[])o.@UnhandledAttributes, o); Write5_XmlSchemaAnnotation((XmlSchemaAnnotation)o.@Annotation); WriteSortedItems(o.@Items); WriteEndElement(); }
} //method GetTypeName private bool ParticleContainsElement(XmlSchemaAll particle, string elementName) { foreach (XmlSchemaObject schemaObject in particle.Items) { if (schemaObject.GetType() == typeof(XmlSchemaElement)) { XmlSchemaElement element = (XmlSchemaElement)schemaObject; if (element.Name == elementName) { return(true); } //if } //if } //foreach return(false); } //method ParticleContainsElement
/// <summary> /// Gets the child elements of the given XML particle that are eligible to /// be the next element in the XML document. /// </summary> /// <param name="p_xspParticle">The particle whoe children are to be retrieved.</param> /// <returns>A list of the child elements of the given XML particle that are eligible to /// be the next element in the XML document.</returns> private List <KeyValuePair <string, string> > GetChildrenElements(XmlSchemaParticle p_xspParticle) { if (p_xspParticle is XmlSchemaElement) { XmlSchemaElement xseElement = (XmlSchemaElement)p_xspParticle; return(new List <KeyValuePair <string, string> >() { new KeyValuePair <string, string>(xseElement.Name, GetDocumentation(xseElement)) }); } else if (p_xspParticle is XmlSchemaSequence) { XmlSchemaSequence xssSequence = (XmlSchemaSequence)p_xspParticle; List <KeyValuePair <string, string> > lstChoices = new List <KeyValuePair <string, string> >(); foreach (XmlSchemaParticle xspParticle in xssSequence.Items) { lstChoices.AddRange(GetChildrenElements(xspParticle)); if (xspParticle.MinOccurs > 0) { break; } } return(lstChoices); } else if (p_xspParticle is XmlSchemaChoice) { XmlSchemaChoice xscChoice = (XmlSchemaChoice)p_xspParticle; List <KeyValuePair <string, string> > lstChoices = new List <KeyValuePair <string, string> >(); foreach (XmlSchemaParticle xspParticle in xscChoice.Items) { lstChoices.AddRange(GetChildrenElements(xspParticle)); } return(lstChoices); } else if (p_xspParticle is XmlSchemaAll) { XmlSchemaAll xsaAll = (XmlSchemaAll)p_xspParticle; List <KeyValuePair <string, string> > lstChoices = new List <KeyValuePair <string, string> >(); foreach (XmlSchemaParticle xspParticle in xsaAll.Items) { lstChoices.AddRange(GetChildrenElements(xspParticle)); } return(lstChoices); } return(null); }
private XmlSchemaGroupBase AddType(XmlSchema schema, XmlSchemaGroupBase container, string name, string typeName, string description) { var element = new XmlSchemaElement(); element.Name = name; element.SchemaTypeName = new XmlQualifiedName(typeName); var type = new XmlSchemaComplexType(); type.Name = typeName; var all = new XmlSchemaAll(); schema.Items.Add(type); container.Items.Add(element); type.Particle = all; AddAnnotation(type, description); return(all); }
/// <summary> /// Declares the ability to have specific relationship instances that connect a 'from' to a 'to'. /// </summary> /// <param name="entityType">The type entity.</param> private void AddRelationshipInstanceElement(EffectiveRelationship entityType, XmlSchema xsd) { // Example: //<xs:element name="worksFor.instance" type="ri_worksFor" /> var typeElement = new XmlSchemaElement(); typeElement.Name = NameDeclared(entityType.Alias.Value + XmlParser.RelationshipInstanceSuffix, entityType.Alias.Namespace); typeElement.SchemaTypeName = entityType.Alias.ToQualifiedName(prefix: "ri_"); xsd.Items.Add(typeElement); // Example of member declaration // This contains both fields and relationships (and reverse relationships) //<xs:complexType name="ri_worksFor"> // <xs:all> // <!-- explicit relationships --> // <xs:element minOccurs="1" maxOccurs="1" name="from" type="is_fromType" /> // <xs:element minOccurs="1" maxOccurs="1" name="to" type="is_toType" /> // </xs:all> //</xs:complexType> // Create a complex type, named "ri_%typename%" var typeMembers = new XmlSchemaComplexType(); typeMembers.Name = NameDeclared("ri_" + entityType.Alias.Value, entityType.Alias.Namespace); // Create a group to hold the members var memberGroup = new XmlSchemaAll(); typeMembers.Particle = memberGroup; // Add from/to if (entityType.IsReverseAlias) { AddRelationshipInstanceMember(entityType, memberGroup, "to", _schemaManager.GetRelationshipFromType(entityType.Type), required: true); AddRelationshipInstanceMember(entityType, memberGroup, "from", _schemaManager.GetRelationshipToType(entityType.Type), required: true); } else { AddRelationshipInstanceMember(entityType, memberGroup, "from", _schemaManager.GetRelationshipFromType(entityType.Type), required: true); AddRelationshipInstanceMember(entityType, memberGroup, "to", _schemaManager.GetRelationshipToType(entityType.Type), required: true); } xsd.Items.Add(typeMembers); }
private XmlSchemaParticle GenerateSchemaElementsForMembers(IXmlTypeSerialiser typeSerialiser) { XmlSchemaAll group = new XmlSchemaAll(); foreach (IXmlMemberSerialiser memberSerialiser in typeSerialiser.MemberSerialisers) { var item = new XmlSchemaElement { MaxOccurs = 1, MinOccurs = memberSerialiser.Attribute.Required ? 1 : 0 }; AddDocumentation(memberSerialiser.Attribute.Description, item); item.Name = memberSerialiser.Attribute.Name; GenerateElementType(memberSerialiser.ReflectorMember.MemberType, item, memberSerialiser.Attribute.HasCustomFactory); group.Items.Add(item); } return(group); }
/// <summary> /// Declares 'field' members for a type. /// That is, all fields that apply to this type, either directly or through inheritance. /// </summary> /// <param name="entityType">The type entity.</param> private void AddTypeMembers(EffectiveType entityType, XmlSchema xsd) { // Example of member declaration // This contains both fields and relationships (and reverse relationships) //<xs:complexType name="type_person"> // <xs:all> // <!-- fields --> // <xs:element minOccurs="0" maxOccurs="1" ref="name" /> // <xs:element minOccurs="0" maxOccurs="1" ref="description" /> // <xs:element minOccurs="0" maxOccurs="1" ref="alias" /> // <xs:element minOccurs="0" maxOccurs="1" ref="firstName" /> // <xs:element minOccurs="0" maxOccurs="1" ref="lastName" /> // <!-- relationships --> // <xs:element minOccurs="0" maxOccurs="1" ref="worksFor" /> // </xs:all> //</xs:complexType> // Create a complex type, named "type_%typename%" var typeMembers = new XmlSchemaComplexType(); typeMembers.Name = NameDeclared("type_" + entityType.Alias.Value, entityType.Alias.Namespace); // Create a group to hold the members var memberGroup = new XmlSchemaAll(); typeMembers.Particle = memberGroup; // Add members AddTypeFields(entityType, memberGroup); AddTypeRelationships(entityType, memberGroup); // Add 'source' attribute for file types Entity file = _aliasResolver[Aliases2.File]; if (_schemaManager.IsSameOrDerivedType(entityType.Type, file)) { XmlSchemaAttribute sourceAttrib = new XmlSchemaAttribute(); sourceAttrib.Name = "source"; sourceAttrib.SchemaTypeName = XmlSchemaSimpleType.GetBuiltInSimpleType(XmlTypeCode.String).QualifiedName; typeMembers.Attributes.Add(sourceAttrib); } xsd.Items.Add(typeMembers); }
protected internal override void Write(XmlSchemaObject obj) { var type = obj as XmlSchemaComplexType; if (type != null) { type.Name = Name; var all = new XmlSchemaAll(); foreach (var prop in Properties) { var element = new XmlSchemaElement(); prop.Write(element); all.Items.Add(element); } if (BaseType != null) { type.ContentModel = new XmlSchemaComplexContent { Content = new XmlSchemaComplexContentExtension { BaseTypeName = BaseType.QualifiedName, AnyAttribute = AnyAttribute, Particle = all } }; } else { type.AnyAttribute = AnyAttribute; type.Particle = all; } } base.Write(obj); }
internal static XmlQualifiedName NameOf(XmlSchemaObject o) { if (o is XmlSchemaAttribute) { return(((XmlSchemaAttribute)o).QualifiedName); } else if (o is XmlSchemaAttributeGroup) { return(((XmlSchemaAttributeGroup)o).QualifiedName); } else if (o is XmlSchemaComplexType) { return(((XmlSchemaComplexType)o).QualifiedName); } else if (o is XmlSchemaSimpleType) { return(((XmlSchemaSimpleType)o).QualifiedName); } else if (o is XmlSchemaElement) { return(((XmlSchemaElement)o).QualifiedName); } else if (o is XmlSchemaGroup) { return(((XmlSchemaGroup)o).QualifiedName); } else if (o is XmlSchemaGroupRef) { return(((XmlSchemaGroupRef)o).RefName); } else if (o is XmlSchemaNotation) { return(((XmlSchemaNotation)o).QualifiedName); } else if (o is XmlSchemaSequence) { XmlSchemaSequence s = (XmlSchemaSequence)o; if (s.Items.Count == 0) { return(new XmlQualifiedName(".sequence", Namespace(o))); } return(NameOf(s.Items[0])); } else if (o is XmlSchemaAll) { XmlSchemaAll a = (XmlSchemaAll)o; if (a.Items.Count == 0) { return(new XmlQualifiedName(".all", Namespace(o))); } return(NameOf(a.Items)); } else if (o is XmlSchemaChoice) { XmlSchemaChoice c = (XmlSchemaChoice)o; if (c.Items.Count == 0) { return(new XmlQualifiedName(".choice", Namespace(o))); } return(NameOf(c.Items)); } else if (o is XmlSchemaAny) { return(new XmlQualifiedName("*", SchemaObjectWriter.ToString(((XmlSchemaAny)o).NamespaceList))); } else if (o is XmlSchemaIdentityConstraint) { return(((XmlSchemaIdentityConstraint)o).QualifiedName); } return(new XmlQualifiedName("?", Namespace(o))); }
private void AddXMLProperties(XmlSchemaElement parent, XmlSchemaObjectCollection items, XmlSchemaObjectCollection attributes, PropertyInfo[] properties) { foreach (PropertyInfo property in properties) { if (IsValidProperty(property)) { string propertyName = property.Name; if (propertyName.Equals("Item") && property.DeclaringType.Name.StartsWith("List`")) { if (!parent.Name.Equals("Items")) { propertyName = StringUtils.ToSingular(parent.Name); } else { propertyName = property.PropertyType.Name; } } string xmlPropertyType = GetPropertyTypeName(property.PropertyType, TDataExchangeFormat.Xml); if (property.GetCustomAttributes <XmlAttributeAttribute>().FirstOrDefault() != null) { XmlSchemaAttribute attribute = new XmlSchemaAttribute(); attribute.Name = propertyName; attribute.SchemaTypeName = new XmlQualifiedName(xmlPropertyType, XML_NAMESPACE); if (attribute.Name.Equals("type")) { attribute.Use = XmlSchemaUse.Optional; } attributes.Add(attribute); } else { XmlSchemaElement propertyElement = new XmlSchemaElement(); propertyElement.Name = propertyName; if (xmlPropertyType.Equals("array") || xmlPropertyType.Equals("object")) { XmlSchemaComplexType complexType = new XmlSchemaComplexType(); propertyElement.SchemaType = complexType; XmlSchemaGroupBase sequence = null; if (xmlPropertyType.Equals("array")) { sequence = new XmlSchemaSequence(); sequence.MinOccursString = "0"; sequence.MaxOccursString = "unbounded"; if (parent != null) { // nested empty collections shouldn't have to exist. propertyElement.UnhandledAttributes = new XmlAttribute[1]; propertyElement.UnhandledAttributes[0] = new XmlDocument().CreateAttribute("minOccurs"); propertyElement.UnhandledAttributes[0].Value = "0"; } } else { sequence = new XmlSchemaAll(); } AddXMLProperties(propertyElement, sequence.Items, complexType.Attributes, property.PropertyType.GetProperties()); if (sequence.Items.Count > 0) { complexType.Particle = sequence; } } else { propertyElement.SchemaTypeName = new XmlQualifiedName(xmlPropertyType, XML_NAMESPACE); } items.Add(propertyElement); } } } }
public virtual void Check(ConformanceCheckContext ctx, XmlSchemaAll value) { }
public static void Main() { XmlSchema schema = new XmlSchema(); XmlSchemaElement thing1 = new XmlSchemaElement(); thing1.Name = "thing1"; thing1.SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema"); schema.Items.Add(thing1); XmlSchemaElement thing2 = new XmlSchemaElement(); thing2.Name = "thing2"; thing2.SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema"); schema.Items.Add(thing2); XmlSchemaElement thing3 = new XmlSchemaElement(); thing3.Name = "thing3"; thing3.SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema"); schema.Items.Add(thing3); XmlSchemaElement thing4 = new XmlSchemaElement(); thing4.Name = "thing4"; thing4.SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema"); schema.Items.Add(thing4); XmlSchemaAttribute myAttribute = new XmlSchemaAttribute(); myAttribute.Name = "myAttribute"; myAttribute.SchemaTypeName = new XmlQualifiedName("decimal", "http://www.w3.org/2001/XMLSchema"); schema.Items.Add(myAttribute); XmlSchemaComplexType myComplexType = new XmlSchemaComplexType(); myComplexType.Name = "myComplexType"; XmlSchemaAll complexType_all = new XmlSchemaAll(); XmlSchemaElement complexType_all_thing1 = new XmlSchemaElement(); complexType_all_thing1.RefName = new XmlQualifiedName("thing1", ""); complexType_all.Items.Add(complexType_all_thing1); XmlSchemaElement complexType_all_thing2 = new XmlSchemaElement(); complexType_all_thing2.RefName = new XmlQualifiedName("thing2", ""); complexType_all.Items.Add(complexType_all_thing2); XmlSchemaElement complexType_all_thing3 = new XmlSchemaElement(); complexType_all_thing3.RefName = new XmlQualifiedName("thing3", ""); complexType_all.Items.Add(complexType_all_thing3); XmlSchemaElement complexType_all_thing4 = new XmlSchemaElement(); complexType_all_thing4.RefName = new XmlQualifiedName("thing4", ""); complexType_all.Items.Add(complexType_all_thing4); myComplexType.Particle = complexType_all; XmlSchemaAttribute complexType_myAttribute = new XmlSchemaAttribute(); complexType_myAttribute.RefName = new XmlQualifiedName("myAttribute", ""); myComplexType.Attributes.Add(complexType_myAttribute); schema.Items.Add(myComplexType); XmlSchemaSet schemaSet = new XmlSchemaSet(); schemaSet.ValidationEventHandler += new ValidationEventHandler(ValidationCallbackOne); schemaSet.Add(schema); schemaSet.Compile(); XmlSchema compiledSchema = null; foreach (XmlSchema schema1 in schemaSet.Schemas()) { compiledSchema = schema1; } XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable()); nsmgr.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema"); compiledSchema.Write(Console.Out, nsmgr); }
static void Check(ConformanceCheckContext ctx, ConformanceChecker checker, Hashtable visitedObjects, XmlSchemaObject value) { if (value == null) { return; } if (visitedObjects.Contains(value)) { return; } visitedObjects.Add(value, value); if (value is XmlSchemaImport) { XmlSchemaImport so = (XmlSchemaImport)value; checker.Check(ctx, so); } else if (value is XmlSchemaAll) { XmlSchemaAll so = (XmlSchemaAll)value; checker.Check(ctx, so); CheckObjects(ctx, checker, visitedObjects, so.Items); } else if (value is XmlSchemaAnnotation) { XmlSchemaAnnotation so = (XmlSchemaAnnotation)value; checker.Check(ctx, so); CheckObjects(ctx, checker, visitedObjects, so.Items); } else if (value is XmlSchemaAttribute) { XmlSchemaAttribute so = (XmlSchemaAttribute)value; checker.Check(ctx, so); } else if (value is XmlSchemaAttributeGroup) { XmlSchemaAttributeGroup so = (XmlSchemaAttributeGroup)value; checker.Check(ctx, so); CheckObjects(ctx, checker, visitedObjects, so.Attributes); Check(ctx, checker, visitedObjects, so.AnyAttribute); Check(ctx, checker, visitedObjects, so.RedefinedAttributeGroup); } else if (value is XmlSchemaAttributeGroupRef) { XmlSchemaAttributeGroupRef so = (XmlSchemaAttributeGroupRef)value; checker.Check(ctx, so); } else if (value is XmlSchemaChoice) { XmlSchemaChoice so = (XmlSchemaChoice)value; checker.Check(ctx, so); CheckObjects(ctx, checker, visitedObjects, so.Items); } else if (value is XmlSchemaComplexContent) { XmlSchemaComplexContent so = (XmlSchemaComplexContent)value; checker.Check(ctx, so); Check(ctx, checker, visitedObjects, so.Content); } else if (value is XmlSchemaComplexContentExtension) { XmlSchemaComplexContentExtension so = (XmlSchemaComplexContentExtension)value; checker.Check(ctx, so); Check(ctx, checker, visitedObjects, so.Particle); CheckObjects(ctx, checker, visitedObjects, so.Attributes); Check(ctx, checker, visitedObjects, so.AnyAttribute); } else if (value is XmlSchemaComplexContentRestriction) { XmlSchemaComplexContentRestriction so = (XmlSchemaComplexContentRestriction)value; checker.Check(ctx, so); Check(ctx, checker, visitedObjects, so.Particle); CheckObjects(ctx, checker, visitedObjects, so.Attributes); Check(ctx, checker, visitedObjects, so.AnyAttribute); } else if (value is XmlSchemaComplexType) { XmlSchemaComplexType so = (XmlSchemaComplexType)value; checker.Check(ctx, so); Check(ctx, checker, visitedObjects, so.ContentModel); Check(ctx, checker, visitedObjects, so.Particle); CheckObjects(ctx, checker, visitedObjects, so.Attributes); Check(ctx, checker, visitedObjects, so.AnyAttribute); Check(ctx, checker, visitedObjects, so.ContentTypeParticle); Check(ctx, checker, visitedObjects, so.AttributeWildcard); } else if (value is XmlSchemaElement) { XmlSchemaElement so = (XmlSchemaElement)value; checker.Check(ctx, so); Check(ctx, checker, visitedObjects, so.SchemaType); CheckObjects(ctx, checker, visitedObjects, so.Constraints); } else if (value is XmlSchemaGroup) { XmlSchemaGroup so = (XmlSchemaGroup)value; checker.Check(ctx, so); Check(ctx, checker, visitedObjects, so.Particle); } else if (value is XmlSchemaGroupRef) { XmlSchemaGroupRef so = (XmlSchemaGroupRef)value; checker.Check(ctx, so); } else if (value is XmlSchemaIdentityConstraint) { XmlSchemaIdentityConstraint so = (XmlSchemaIdentityConstraint)value; checker.Check(ctx, so); CheckObjects(ctx, checker, visitedObjects, so.Fields); Check(ctx, checker, visitedObjects, so.Selector); } else if (value is XmlSchemaKeyref) { XmlSchemaKeyref so = (XmlSchemaKeyref)value; checker.Check(ctx, so); } else if (value is XmlSchemaRedefine) { XmlSchemaRedefine so = (XmlSchemaRedefine)value; checker.Check(ctx, so); CheckObjects(ctx, checker, visitedObjects, so.Items); } else if (value is XmlSchemaSequence) { XmlSchemaSequence so = (XmlSchemaSequence)value; checker.Check(ctx, so); CheckObjects(ctx, checker, visitedObjects, so.Items); } else if (value is XmlSchemaSimpleContent) { XmlSchemaSimpleContent so = (XmlSchemaSimpleContent)value; checker.Check(ctx, so); Check(ctx, checker, visitedObjects, so.Content); } else if (value is XmlSchemaSimpleContentExtension) { XmlSchemaSimpleContentExtension so = (XmlSchemaSimpleContentExtension)value; checker.Check(ctx, so); CheckObjects(ctx, checker, visitedObjects, so.Attributes); Check(ctx, checker, visitedObjects, so.AnyAttribute); } else if (value is XmlSchemaSimpleContentRestriction) { XmlSchemaSimpleContentRestriction so = (XmlSchemaSimpleContentRestriction)value; checker.Check(ctx, so); CheckObjects(ctx, checker, visitedObjects, so.Attributes); Check(ctx, checker, visitedObjects, so.AnyAttribute); CheckObjects(ctx, checker, visitedObjects, so.Facets); } else if (value is XmlSchemaSimpleType) { XmlSchemaSimpleType so = (XmlSchemaSimpleType)value; checker.Check(ctx, so); Check(ctx, checker, visitedObjects, so.Content); } else if (value is XmlSchemaSimpleTypeList) { XmlSchemaSimpleTypeList so = (XmlSchemaSimpleTypeList)value; checker.Check(ctx, so); } else if (value is XmlSchemaSimpleTypeRestriction) { XmlSchemaSimpleTypeRestriction so = (XmlSchemaSimpleTypeRestriction)value; checker.Check(ctx, so); CheckObjects(ctx, checker, visitedObjects, so.Facets); } else if (value is XmlSchemaSimpleTypeUnion) { XmlSchemaSimpleTypeUnion so = (XmlSchemaSimpleTypeUnion)value; checker.Check(ctx, so); } }
protected override void Visit(XmlSchemaAll particle) { // Don't visit children. }
void PopulateComplexType(XmlSchemaComplexType complexType, Type type) { if (type.IsAbstract) { complexType.IsAbstract = true; } XmlSchemaGroupBase schemaParticle = new XmlSchemaSequence(); foreach (var pi in type.GetProperties()) { if (pi.DeclaringType != type) { continue; } var textAttr = pi.GetAttributes <XmlTextAttribute>().FirstOrDefault(); if (textAttr != null) { complexType.IsMixed = true; continue; } var attrAttr = pi.GetAttributes <XmlAttributeAttribute>().FirstOrDefault(); if (attrAttr != null) { var attr = MakeAttribute(attrAttr.AttributeName, pi); complexType.Attributes.Add(attr); continue; } var anyAttr = pi.GetAttributes <XmlAnyAttributeAttribute>().FirstOrDefault(); if (anyAttr != null) { complexType.AnyAttribute = new XmlSchemaAnyAttribute() { ProcessContents = XmlSchemaContentProcessing.Skip }; continue; } var elemAttrs = pi.GetAttributes <XmlElementAttribute>(); if (elemAttrs.Skip(1).Any()) { // If there is more than 1 XmlElement attribute, make a sequence of choice of elements var elemChoice = new XmlSchemaChoice(); foreach (var elemAttr in elemAttrs) { var elems = MakeElement(elemAttr.ElementName, elemAttr.Type, pi, null); foreach (var elem in elems) { elemChoice.MinOccursString = elem.MinOccursString; elemChoice.MaxOccursString = elem.MaxOccursString; elem.MinOccursString = "0"; elem.MaxOccursString = "1"; elemChoice.Items.Add(elem); } } schemaParticle.Items.Add(elemChoice); } else if (elemAttrs.Any()) { var elemAttr = elemAttrs.First(); var elems = MakeElement(elemAttr.ElementName, null, pi, null); foreach (var elem in elems) { schemaParticle.Items.Add(elem); } continue; } var pluginAttr = pi.GetAttributes <PluginElementAttribute>().FirstOrDefault(); if (pluginAttr != null) { var elems = MakeElement(pluginAttr.ElementName, null, pi, pluginAttr); foreach (var elem in elems) { schemaParticle.Items.Add(elem); } continue; } } // See if there is a better way to do this. // If particle contains a single element, use xs:sequence // If all items are [0|1,1], use xs:all // Else, use xs:choice [0, unbounded] while (schemaParticle.Items.Count > 1) { var items = schemaParticle.Items.OfType <XmlSchemaParticle>(); if (!items.Where(i => i.MaxOccursString != "1").Any()) { // All maxOccurs are "1", check minOccurs... var disinct = items.Select(a => a.MinOccursString).Distinct(); var value = disinct.FirstOrDefault(); var moreThanOne = disinct.Skip(1).Any(); if (!moreThanOne && (value == "0" || value == "1")) { // minOccurs is "0" or "1", convert to xs:all var allParticle = new XmlSchemaAll(); allParticle.MinOccursString = value; allParticle.MaxOccursString = "1"; foreach (var item in schemaParticle.Items) { allParticle.Items.Add(item); } schemaParticle = allParticle; break; } } var choiceParticle = new XmlSchemaChoice(); choiceParticle.MinOccursString = "0"; choiceParticle.MaxOccursString = "unbounded"; foreach (var item in items) { item.MinOccursString = null; item.MaxOccursString = null; choiceParticle.Items.Add(item); } schemaParticle = choiceParticle; break; } if (type.BaseType != typeof(object) && type.BaseType != null) { var ext = new XmlSchemaComplexContentExtension(); ext.BaseTypeName = new XmlQualifiedName(type.BaseType.Name, schema.TargetNamespace); foreach (var attr in complexType.Attributes) { ext.Attributes.Add(attr); } complexType.Attributes.Clear(); if (schemaParticle.Items.Count > 0) { ext.Particle = schemaParticle; } var content = new XmlSchemaComplexContent(); content.IsMixed = false; content.Content = ext; complexType.ContentModel = content; } else if (schemaParticle.Items.Count > 0) { complexType.Particle = schemaParticle; } }
private static XmlSchemaElement CaseSchema() { XmlSchemaComplexType type = new XmlSchemaComplexType(); XmlSchemaAll allSchema = new XmlSchemaAll(); type.Particle = allSchema; //When XmlSchemaComplexType subType = new XmlSchemaComplexType(); XmlSchemaAny any = new XmlSchemaAny(); any.MinOccurs = 1; any.MaxOccursString = "unbounded"; any.ProcessContents = XmlSchemaContentProcessing.Strict; any.Namespace = "##local"; XmlSchemaSequence sequence = new XmlSchemaSequence(); subType.Particle = sequence; sequence.Items.Add(any); XmlSchemaElement subElement = new XmlSchemaElement(); subElement.Name = "when"; subElement.SchemaType = subType; allSchema.Items.Add(subElement); //Then subType = new XmlSchemaComplexType(); any = new XmlSchemaAny(); any.MinOccurs = 1; any.MaxOccursString = "unbounded"; any.ProcessContents = XmlSchemaContentProcessing.Strict; any.Namespace = "##local"; sequence = new XmlSchemaSequence(); subType.Particle = sequence; sequence.Items.Add(any); subElement = new XmlSchemaElement(); subElement.Name = "then"; subElement.SchemaType = subType; allSchema.Items.Add(subElement); //Else subType = new XmlSchemaComplexType(); any = new XmlSchemaAny(); any.MinOccurs = 1; any.MaxOccurs = 1; any.ProcessContents = XmlSchemaContentProcessing.Strict; any.Namespace = "##local"; sequence = new XmlSchemaSequence(); subType.Particle = sequence; sequence.Items.Add(any); subElement = new XmlSchemaElement(); subElement.Name = "else"; subElement.SchemaType = subType; allSchema.Items.Add(subElement); XmlSchemaAttribute attrib = new XmlSchemaAttribute(); attrib.Name = "expressionLanguage"; attrib.Use = XmlSchemaUse.Optional; attrib.SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema"); type.Attributes.Add(attrib); attrib = new XmlSchemaAttribute(); attrib.Name = "failMessage"; attrib.Use = XmlSchemaUse.Optional; attrib.SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema"); type.Attributes.Add(attrib); XmlSchemaElement element = new XmlSchemaElement(); element.Name = "case"; element.SchemaType = type; return(element); }
public static List <XmlSchemaElement> GetAllElements(XmlSchemaObject obj, bool recursive, List <XmlSchemaElement> allElements) { List <XmlSchemaElement> elements = new List <XmlSchemaElement>(); // Element if (obj.GetType().Equals(typeof(XmlSchemaElement))) { XmlSchemaElement element = (XmlSchemaElement)obj; XmlSchemaComplexType complexType = element.ElementSchemaType as XmlSchemaComplexType; if (complexType != null) { #region sequence /// Get the sequence particle of the complex type. XmlSchemaSequence sequence = complexType.ContentTypeParticle as XmlSchemaSequence; if (sequence != null) { // Iterate over each XmlSchemaElement in the Items collection. foreach (XmlSchemaObject childElement in sequence.Items) { elements = GetElements(childElement, elements, recursive, allElements); } } #endregion #region sequence /// Get the sequence particle of the complex type. XmlSchemaAll all = complexType.ContentTypeParticle as XmlSchemaAll; if (all != null) { // Iterate over each XmlSchemaElement in the Items collection. foreach (XmlSchemaObject childElement in all.Items) { elements = GetElements(childElement, elements, recursive, allElements); } } #endregion #region choice // check if it is e choice XmlSchemaChoice choice = complexType.ContentTypeParticle as XmlSchemaChoice; if (choice != null) { // Iterate over each XmlSchemaElement in the Items collection. foreach (XmlSchemaObject childElement in choice.Items) { elements = GetElements(childElement, elements, recursive, allElements); } } #endregion } } if (obj.GetType().Equals(typeof(XmlSchemaComplexType))) { XmlSchemaComplexType complexType = obj as XmlSchemaComplexType; if (complexType != null) { #region sequence /// Get the sequence particle of the complex type. XmlSchemaSequence sequence = complexType.ContentTypeParticle as XmlSchemaSequence; if (sequence != null) { // Iterate over each XmlSchemaElement in the Items collection. foreach (XmlSchemaObject childElement in sequence.Items) { elements = GetElements(childElement, elements, recursive, allElements); } } #endregion #region choice // check if it is e choice XmlSchemaChoice choice = complexType.ContentTypeParticle as XmlSchemaChoice; if (choice != null) { // Iterate over each XmlSchemaElement in the Items collection. foreach (XmlSchemaObject childElement in choice.Items) { elements = GetElements(childElement, elements, recursive, allElements); } } #endregion } } if (obj is XmlSchemaGroup) { XmlSchemaGroup group = obj as XmlSchemaGroup; #region sequence /// Get the sequence particle of the complex type. XmlSchemaSequence sequence = group.Particle as XmlSchemaSequence; if (sequence != null) { // Iterate over each XmlSchemaElement in the Items collection. foreach (XmlSchemaObject childElement in sequence.Items) { elements = GetElements(childElement, elements, recursive, allElements); } } #endregion #region choice // check if it is e choice XmlSchemaChoice choice = group.Particle as XmlSchemaChoice; if (choice != null) { // Iterate over each XmlSchemaElement in the Items collection. foreach (XmlSchemaObject childElement in choice.Items) { elements = GetElements(childElement, elements, recursive, allElements); } } #endregion } //if (obj is XmlSchemaSequence) //{ // // Iterate over each XmlSchemaElement in the Items collection. // foreach (XmlSchemaObject childElement in ((XmlSchemaSequence)obj).Items) // { // elements = GetAllElements(childElement, recursive, allElements); // } //} //if (obj is XmlSchemaChoice) //{ // // Iterate over each XmlSchemaElement in the Items collection. // foreach (XmlSchemaObject childElement in ((XmlSchemaChoice)obj).Items) // { // elements = GetAllElements(childElement, recursive, allElements); // } //} return(elements); }