internal DataTable InstantiateTable(XmlSchemaElement node, XmlSchemaComplexType typeNode, bool isRef)
{
DataTable table;
string typeName = GetInstanceName(node);
ArrayList tableChildren = new ArrayList();
string _TableUri;
_TableUri = node.QualifiedName.Namespace;
table = _ds.Tables.GetTable(XmlConvert.DecodeName(typeName), _TableUri);
// TOD: Do not do this fix
// if (table == null && node.RefName.IsEmpty && !IsTopLevelElement(node) && _TableUri != null && _TableUri.Length > 0) {
// _TableUri = null; // it means form="qualified", so child element inherits namespace. amirhmy
// }
if (!FromInference || (FromInference && table == null))
{
if (table != null)
{
if (isRef)
return table;
else
throw ExceptionBuilder.DuplicateDeclaration(typeName);
}
if (isRef)
_refTables.Add(_TableUri + ":" + typeName);
table = new DataTable(XmlConvert.DecodeName(typeName));
table.TypeName = node.SchemaTypeName;
table.Namespace = _TableUri;
table.Namespace = GetStringAttribute(node, "targetNamespace", _TableUri);
//table.Prefix = node.Prefix;
string value = GetStringAttribute(typeNode, Keywords.MSD_CASESENSITIVE, "");
if (value.Length == 0)
{
value = GetStringAttribute(node, Keywords.MSD_CASESENSITIVE, "");
}
if (0 < value.Length)
{
if ((value == Keywords.TRUE) || (value == "True"))
table.CaseSensitive = true;
if ((value == Keywords.FALSE) || (value == "False"))
table.CaseSensitive = false;
}
value = GetMsdataAttribute(node, Keywords.MSD_LOCALE);
if (null != value)
{ // set by user
if (0 < value.Length)
{
// <... msdata:Locale="en-US"/>
table.Locale = new CultureInfo(value);
}
else
{
table.Locale = CultureInfo.InvariantCulture;
}
}
// else inherit from DataSet, not set by user
if (!FromInference)
{
table.MinOccurs = node.MinOccurs;
table.MaxOccurs = node.MaxOccurs;
}
else
{
string prefix = GetPrefix(_TableUri);
if (prefix != null)
table.Prefix = prefix;
}
_ds.Tables.Add(table);
if (FromInference)
{
_tableDictionary.Add(table, new List<DataTable>());
}
}
HandleComplexType(typeNode, table, tableChildren, node.IsNillable);
for (int i = 0; i < table.Columns.Count; i++)
table.Columns[i].SetOrdinalInternal(i);
/*
if (xmlContent == XmlContent.Mixed) {
string textColumn = GenUniqueColumnName(table.TableName+ "_Text", table);
table.XmlText = new DataColumn(textColumn, typeof(string), null, MappingType.Text);
} */
SetProperties(table, node.UnhandledAttributes);
SetExtProperties(table, node.UnhandledAttributes);
// handle all the unique and key constraints related to this table
if ((_dsElement != null) && (_dsElement.Constraints != null))
{
foreach (XmlSchemaIdentityConstraint key in _dsElement.Constraints)
{
if (key is XmlSchemaKeyref)
continue;
if (GetTableName(key) == table.TableName)
{
// respect the NS if it is specified for key, otherwise just go with table name check
if (GetTableNamespace(key) == table.Namespace || GetTableNamespace(key) == null)
HandleConstraint(key);
/* if (GetTableNamespace(key) != null) {
if (GetTableNamespace(key) == table.Namespace)
HandleConstraint(key);
}
else {
HandleConstraint(key);
}
*/
}
}
}
foreach (DataTable _tableChild in tableChildren)
{
if (_tableChild != table && table.Namespace == _tableChild.Namespace)
_tableChild._tableNamespace = null;
if ((_dsElement != null) && (_dsElement.Constraints != null))
{
foreach (XmlSchemaIdentityConstraint key in _dsElement.Constraints)
{
XmlSchemaKeyref keyref = key as XmlSchemaKeyref;
if (keyref == null)
continue;
bool isNested = GetBooleanAttribute(keyref, Keywords.MSD_ISNESTED, /*default:*/ false);
if (!isNested)
continue;
if (GetTableName(keyref) == _tableChild.TableName)
{
if (_tableChild.DataSet.Tables.InternalIndexOf(_tableChild.TableName) < -1)
{ // if we have multiple tables with the same name
if (GetTableNamespace(keyref) == _tableChild.Namespace)
{
HandleKeyref(keyref);
}
}
else
{
HandleKeyref(keyref);
}
}
}
}
DataRelation relation = null;
DataRelationCollection childRelations = table.ChildRelations;
for (int j = 0; j < childRelations.Count; j++)
{
if (!childRelations[j].Nested)
continue;
if (_tableChild == childRelations[j].ChildTable)
relation = childRelations[j];
}
if (relation != null)
continue;
DataColumn parentKey;
if (FromInference)
{
int position = table.UKColumnPositionForInference;// we keep posiotion of unique key column here, for inference
if (position == -1)
foreach (DataColumn dc in table.Columns)
{
if (dc.ColumnMapping == MappingType.Attribute)
{
position = dc.Ordinal;
break;
}
}
parentKey = table.AddUniqueKey(position);
}
else
{
parentKey = table.AddUniqueKey();
}
// foreign key in the child table
DataColumn childKey = _tableChild.AddForeignKey(parentKey);
// when we add unique key, we do set prefix; but for Fk we do not do . So for backward compatability
if (FromInference)
childKey.Prefix = _tableChild.Prefix;
// childKey.Prefix = GetPrefix(childKey.Namespace);
// create relationship
// setup relationship between parent and this table
relation = new DataRelation(table.TableName + "_" + _tableChild.TableName, parentKey, childKey, true);
relation.Nested = true;
_tableChild.DataSet.Relations.Add(relation);
if (FromInference && relation.Nested)
{
if (_tableDictionary.ContainsKey(relation.ParentTable))
{
_tableDictionary[relation.ParentTable].Add(relation.ChildTable);
}
}
}
return (table);
}