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;
}