public void LoadSchema(XmlSchemaSet schemaSet, DataSet ds)
{ //Element schemaRoot, DataSet ds) {
_constraintNodes = new Hashtable();
_refTables = new ArrayList();
_columnExpressions = new ArrayList();
_complexTypes = new ArrayList();
bool setRootNStoDataSet = false;
bool newDataSet = (ds.Tables.Count == 0);
if (schemaSet == null)
return;
_schemaSet = schemaSet;
_ds = ds;
ds._fIsSchemaLoading = true;
foreach (XmlSchema schemaRoot in schemaSet.Schemas())
{
_schemaName = schemaRoot.Id;
if (_schemaName == null || _schemaName.Length == 0)
{
_schemaName = "NewDataSet";
}
ds.DataSetName = XmlConvert.DecodeName(_schemaName);
string ns = schemaRoot.TargetNamespace;
if (ds._namespaceURI == null || ds._namespaceURI.Length == 0)
{// set just one time, for backward compatibility
ds._namespaceURI = (ns == null) ? string.Empty : ns; // see fx\Data\XDO\ReadXml\SchemaM2.xml for more info
}
break; // we just need to take Name and NS from first schema [V1.0 & v1.1 semantics]
}
_annotations = new XmlSchemaObjectCollection();
_elements = new XmlSchemaObjectCollection();
_elementsTable = new Hashtable();
_attributes = new Hashtable();
_attributeGroups = new Hashtable();
_schemaTypes = new Hashtable();
_tableDictionary = new Dictionary<DataTable, List<DataTable>>();
_existingSimpleTypeMap = new Hashtable();
foreach (DataTable dt in ds.Tables)
{
foreach (DataColumn dc in dt.Columns)
{
if (dc.SimpleType != null && dc.SimpleType.Name != null && dc.SimpleType.Name.Length != 0)
{
_existingSimpleTypeMap[dc.SimpleType.SimpleTypeQualifiedName] = dc;
// existingSimpleTypeMap[dc.SimpleType.SimpleTypeQualifiedName] = dc.SimpleType;
}
}
}
foreach (XmlSchema schemaRoot in schemaSet.Schemas())
CollectElementsAnnotations(schemaRoot);
_dsElement = FindDatasetElement(_elements);
if (_dsElement != null)
{
string mainName = GetStringAttribute(_dsElement, Keywords.MSD_MAINDATATABLE, "");
if (null != mainName)
{
ds.MainTableName = XmlConvert.DecodeName(mainName);
}
}
else
{
if (FromInference)
{
ds._fTopLevelTable = true; // Backward compatability: for inference, if we do not read DataSet element
}
// we should not write it also
setRootNStoDataSet = true;
//incase of Root is not mapped to DataSet and is mapped to DataTable instead; to be backward compatable
// we need to set the Namespace of Root to DataSet's namespace also(it would be NS of First DataTable in collection)
}
List<XmlQualifiedName> qnames = new List<XmlQualifiedName>();
if (ds != null && ds._useDataSetSchemaOnly)
{
int dataSetElementCount = DatasetElementCount(_elements);
if (dataSetElementCount == 0)
{
throw ExceptionBuilder.IsDataSetAttributeMissingInSchema();
}
else if (dataSetElementCount > 1)
{
throw ExceptionBuilder.TooManyIsDataSetAtributeInSchema();
}
XmlSchemaComplexType ct = (XmlSchemaComplexType)FindTypeNode(_dsElement);
if (ct.Particle != null)
{
XmlSchemaObjectCollection items = GetParticleItems(ct.Particle);
if (items != null)
{
foreach (XmlSchemaAnnotated el in items)
{
XmlSchemaElement sel = el as XmlSchemaElement;
if (null != sel)
{
if (sel.RefName.Name.Length != 0)
{
qnames.Add(sel.QualifiedName);
}
}
}
}
}
}
// Walk all the top level Element tags.
foreach (XmlSchemaElement element in _elements)
{
if (element == _dsElement)
continue;
if (ds != null && ds._useDataSetSchemaOnly && _dsElement != null)
{
if (_dsElement.Parent != element.Parent)
{
if (!qnames.Contains(element.QualifiedName))
{
continue;
}
}
}
string typeName = GetInstanceName(element);
if (_refTables.Contains(element.QualifiedName.Namespace + ":" + typeName))
{
HandleRefTableProperties(_refTables, element);
continue;
}
DataTable table = HandleTable(element);
}
if (_dsElement != null)
HandleDataSet(_dsElement, newDataSet);
foreach (XmlSchemaAnnotation annotation in _annotations)
{
HandleRelations(annotation, false);
}
//just add Expressions, at this point and if ColumnExpressions.Count > 0, this.expressions should not be null
for (int i = 0; i < _columnExpressions.Count; i++)
{
DataColumn dc = ((DataColumn)(_columnExpressions[i]));
dc.Expression = (string)_expressions[dc];
}
foreach (DataTable dt in ds.Tables)
{
if (dt.NestedParentRelations.Length == 0 && dt.Namespace == ds.Namespace)
{
DataRelationCollection childRelations = dt.ChildRelations;
for (int j = 0; j < childRelations.Count; j++)
{
// we need to do the same thing for nested child tables as they
if (childRelations[j].Nested && dt.Namespace == childRelations[j].ChildTable.Namespace)
{
// take NS from Parent table
childRelations[j].ChildTable._tableNamespace = null;
}
}
dt._tableNamespace = null;
}
}
DataTable tmpTable = ds.Tables[ds.DataSetName, ds.Namespace];
if (tmpTable != null) // this fix is done to support round-trip problem in case if there is one table with same name and NS
tmpTable._fNestedInDataset = true;
// this fix is for backward compatability with old inference engine
if (FromInference && ds.Tables.Count == 0 && string.Equals(ds.DataSetName, "NewDataSet", StringComparison.Ordinal))
ds.DataSetName = XmlConvert.DecodeName(((XmlSchemaElement)_elements[0]).Name);
ds._fIsSchemaLoading = false; //reactivate column computations
//for backward compatability; we need to set NS of Root Element to DataSet, if root already does not mapped to dataSet
if (setRootNStoDataSet)
{
if (ds.Tables.Count > 0)
{ // if there is table, take first one's NS
ds.Namespace = ds.Tables[0].Namespace;
ds.Prefix = ds.Tables[0].Prefix;
}
else
{// otherwise, take TargetNS from first schema
Debug.Assert(schemaSet.Count == 1, "there should be one schema");
foreach (XmlSchema schemaRoot in schemaSet.Schemas())
{ // we should have 1 schema
ds.Namespace = schemaRoot.TargetNamespace;
}
}
}
}