internal void ReadXmlSchema(XmlReader reader, bool denyResolving)
{
long logScopeId = DataCommonEventSource.Log.EnterScope("<ds.DataTable.ReadXmlSchema|INFO> {0}, denyResolving={1}", ObjectID, denyResolving);
try
{
DataSet ds = new DataSet();
SerializationFormat cachedRemotingFormat = RemotingFormat;
// fxcop: ReadXmlSchema will provide the CaseSensitive, Locale, Namespace information
ds.ReadXmlSchema(reader, denyResolving);
string CurrentTableFullName = ds.MainTableName;
if (string.IsNullOrEmpty(_tableName) && string.IsNullOrEmpty(CurrentTableFullName))
{
return;
}
DataTable currentTable = null;
if (!string.IsNullOrEmpty(_tableName))
{
if (!string.IsNullOrEmpty(Namespace))
{
currentTable = ds.Tables[_tableName, Namespace];
}
else
{
int tableIndex = ds.Tables.InternalIndexOf(_tableName);
if (tableIndex > -1)
{
currentTable = ds.Tables[tableIndex];
}
}
}
else
{
string CurrentTableNamespace = string.Empty;
int nsSeperator = CurrentTableFullName.IndexOf(':');
if (nsSeperator > -1)
{
CurrentTableNamespace = CurrentTableFullName.Substring(0, nsSeperator);
}
string CurrentTableName = CurrentTableFullName.Substring(nsSeperator + 1, CurrentTableFullName.Length - nsSeperator - 1);
currentTable = ds.Tables[CurrentTableName, CurrentTableNamespace];
}
if (currentTable == null)
{
string qTableName = string.Empty;
if (!string.IsNullOrEmpty(_tableName))
{
qTableName = (Namespace.Length > 0) ? (Namespace + ":" + _tableName) : _tableName;
}
else
{
qTableName = CurrentTableFullName;
}
throw ExceptionBuilder.TableNotFound(qTableName);
}
currentTable._remotingFormat = cachedRemotingFormat;
List<DataTable> tableList = new List<DataTable>();
tableList.Add(currentTable);
CreateTableList(currentTable, tableList);
List<DataRelation> relationList = new List<DataRelation>();
CreateRelationList(tableList, relationList);
if (relationList.Count == 0)
{
if (Columns.Count == 0)
{
DataTable tempTable = currentTable;
if (tempTable != null)
{
tempTable.CloneTo(this, null, false); // we may have issue Amir
}
if (DataSet == null && _tableNamespace == null)
{
// for standalone table, clone wont get these correctly, since they may come with inheritance
_tableNamespace = tempTable.Namespace;
}
}
return;
}
else
{
if (string.IsNullOrEmpty(TableName))
{
TableName = currentTable.TableName;
if (!string.IsNullOrEmpty(currentTable.Namespace))
{
Namespace = currentTable.Namespace;
}
}
if (DataSet == null)
{
DataSet dataset = new DataSet(ds.DataSetName);
// if user set values on DataTable, it isn't necessary
// to set them on the DataSet because they won't be inherited
// but it is simpler to set them in both places
// if user did not set values on DataTable, it is required
// to set them on the DataSet so the table will inherit
// the value already on the Datatable
dataset.SetLocaleValue(ds.Locale, ds.ShouldSerializeLocale());
dataset.CaseSensitive = ds.CaseSensitive;
dataset.Namespace = ds.Namespace;
dataset._mainTableName = ds._mainTableName;
dataset.RemotingFormat = ds.RemotingFormat;
dataset.Tables.Add(this);
}
DataTable targetTable = CloneHierarchy(currentTable, DataSet, null);
foreach (DataTable tempTable in tableList)
{
DataTable destinationTable = DataSet.Tables[tempTable._tableName, tempTable.Namespace];
DataTable sourceTable = ds.Tables[tempTable._tableName, tempTable.Namespace];
foreach (Constraint tempConstrain in sourceTable.Constraints)
{
ForeignKeyConstraint fkc = tempConstrain as ForeignKeyConstraint; // we have already cloned the UKC when cloning the datatable
if (fkc != null)
{
if (fkc.Table != fkc.RelatedTable)
{
if (tableList.Contains(fkc.Table) && tableList.Contains(fkc.RelatedTable))
{
ForeignKeyConstraint newFKC = (ForeignKeyConstraint)fkc.Clone(destinationTable.DataSet);
if (!destinationTable.Constraints.Contains(newFKC.ConstraintName))
{
destinationTable.Constraints.Add(newFKC); // we know that the dest table is already in the table
}
}
}
}
}
}
foreach (DataRelation rel in relationList)
{
if (!DataSet.Relations.Contains(rel.RelationName))
{
DataSet.Relations.Add(rel.Clone(DataSet));
}
}
bool hasExternaldependency = false;
foreach (DataTable tempTable in tableList)
{
foreach (DataColumn dc in tempTable.Columns)
{
hasExternaldependency = false;
if (dc.Expression.Length != 0)
{
DataColumn[] dependency = dc.DataExpression.GetDependency();
for (int j = 0; j < dependency.Length; j++)
{
if (!tableList.Contains(dependency[j].Table))
{
hasExternaldependency = true;
break;
}
}
}
if (!hasExternaldependency)
{
DataSet.Tables[tempTable.TableName, tempTable.Namespace].Columns[dc.ColumnName].Expression = dc.Expression;
}
}
hasExternaldependency = false;
}
}
}
finally
{
DataCommonEventSource.Log.ExitScope(logScopeId);
}
}