private XmlSchemaElement AddElement(string localName, string prefix, string childURI, XmlSchema parentSchema, XmlSchemaObjectCollection addLocation, int positionWithinCollection) { if (childURI == XmlSchema.Namespace) { throw new XmlSchemaInferenceException(Res.SchInf_schema, 0, 0); } XmlSchemaElement xse = null; XmlSchemaElement returnedElement = xse; //this value will change to elementReference if childURI!= parentURI XmlSchema xs = null; bool bCreatingNewType = true; if (childURI == String.Empty) { childURI = null; } // The new element belongs to the same ns as parent and addlocation is not null if (parentSchema != null && childURI == parentSchema.TargetNamespace) { xse = new XmlSchemaElement(); xse.Name = localName; xs = parentSchema; if (xs.ElementFormDefault != XmlSchemaForm.Qualified && addLocation != null) { xse.Form = XmlSchemaForm.Qualified; } } else if (schemaSet.Contains(childURI)) { xse = this.FindGlobalElement(childURI, localName, out xs); if (xse == null) { ArrayList col = this.schemaSet.Schemas(childURI)as ArrayList; if (col != null && col.Count > 0 ) { xs = col[0] as XmlSchema; } xse = new XmlSchemaElement(); xse.Name = localName; xs.Items.Add(xse); } else bCreatingNewType = false; } else { xs = CreateXmlSchema(childURI); if (prefix.Length!=0) NamespaceManager.AddNamespace(prefix, childURI); xse=new XmlSchemaElement(); xse.Name = localName; xs.Items.Add(xse); //add global element declaration only when creating new schema } if (parentSchema == null) { parentSchema = xs; this.rootSchema = parentSchema; } if (childURI != parentSchema.TargetNamespace ) { bool add = true; for (int i = 0; i < parentSchema.Includes.Count; ++i) { XmlSchemaImport import = parentSchema.Includes[i] as XmlSchemaImport; if (import == null) { continue; } //Debug.WriteLine(import.Schema.TargetNamespace); if (import.Namespace == childURI) { add = false; } } if (add) { XmlSchemaImport import = new XmlSchemaImport(); import.Schema = xs; import.Namespace = childURI; parentSchema.Includes.Add(import); } } returnedElement = xse; if (addLocation != null) { if (childURI == parentSchema.TargetNamespace ) { if (this.Occurrence == InferenceOption.Relaxed /*&& parentSchema.Items != addLocation*/) { xse.MinOccurs = 0; } if (positionWithinCollection == -1) { positionWithinCollection = addLocation.Add(xse); } else { addLocation.Insert(positionWithinCollection, xse); } } else { XmlSchemaElement elementReference = new XmlSchemaElement(); elementReference.RefName = new XmlQualifiedName(localName, childURI); if (this.Occurrence == InferenceOption.Relaxed) { elementReference.MinOccurs = 0; } if (positionWithinCollection == -1) { positionWithinCollection = addLocation.Add(elementReference); } else { addLocation.Insert(positionWithinCollection, elementReference); } returnedElement = elementReference; /* if (childURI == XmlSchema.Namespace) { schemaList.Add(new ReplaceList(addLocation, positionWithinCollection)); }*/ } } InferElement(xse, bCreatingNewType, xs); return returnedElement; }
private XmlSchemaElement AddElement(string localName, string prefix, string childURI, XmlSchema parentSchema, XmlSchemaObjectCollection addLocation, int positionWithinCollection) { if (childURI == "http://www.w3.org/2001/XMLSchema") { throw new XmlSchemaInferenceException("SchInf_schema", 0, 0); } XmlSchemaElement item = null; XmlSchemaElement element2 = item; XmlSchema schema = null; bool bCreatingNewType = true; if (childURI == string.Empty) { childURI = null; } if ((parentSchema != null) && (childURI == parentSchema.TargetNamespace)) { item = new XmlSchemaElement { Name = localName }; schema = parentSchema; if ((schema.ElementFormDefault != XmlSchemaForm.Qualified) && (addLocation != null)) { item.Form = XmlSchemaForm.Qualified; } } else if (this.schemaSet.Contains(childURI)) { item = this.FindGlobalElement(childURI, localName, out schema); if (item == null) { ArrayList list = this.schemaSet.Schemas(childURI) as ArrayList; if ((list != null) && (list.Count > 0)) { schema = list[0] as XmlSchema; } item = new XmlSchemaElement { Name = localName }; schema.Items.Add(item); } else { bCreatingNewType = false; } } else { schema = this.CreateXmlSchema(childURI); if (prefix.Length != 0) { this.NamespaceManager.AddNamespace(prefix, childURI); } item = new XmlSchemaElement { Name = localName }; schema.Items.Add(item); } if (parentSchema == null) { parentSchema = schema; this.rootSchema = parentSchema; } if (childURI != parentSchema.TargetNamespace) { bool flag2 = true; for (int i = 0; i < parentSchema.Includes.Count; i++) { XmlSchemaImport import = parentSchema.Includes[i] as XmlSchemaImport; if ((import != null) && (import.Namespace == childURI)) { flag2 = false; } } if (flag2) { XmlSchemaImport import2 = new XmlSchemaImport { Schema = schema, Namespace = childURI }; parentSchema.Includes.Add(import2); } } element2 = item; if (addLocation != null) { if (childURI == parentSchema.TargetNamespace) { if (this.Occurrence == InferenceOption.Relaxed) { item.MinOccurs = 0M; } if (positionWithinCollection == -1) { positionWithinCollection = addLocation.Add(item); } else { addLocation.Insert(positionWithinCollection, item); } } else { XmlSchemaElement element3 = new XmlSchemaElement { RefName = new XmlQualifiedName(localName, childURI) }; if (this.Occurrence == InferenceOption.Relaxed) { element3.MinOccurs = 0M; } if (positionWithinCollection == -1) { positionWithinCollection = addLocation.Add(element3); } else { addLocation.Insert(positionWithinCollection, element3); } element2 = element3; } } this.InferElement(item, bCreatingNewType, schema); return element2; }