private void ReadXml ()
{
if (document.DocumentElement == null)
return;
dataset.Locale = new CultureInfo ("en-US"); // default(!)
// If the root element is not a data table, treat
// this element as DataSet.
// Read one element. It might be DataSet element.
XmlElement el = document.DocumentElement;
if (el.NamespaceURI == XmlSchema.Namespace)
throw new InvalidOperationException ("DataSet is not designed to handle XML Schema as data content. Please use ReadXmlSchema method instead of InferXmlSchema method.");
if (IsDocumentElementTable ())
InferTopLevelTable (el);
else {
string localName = XmlHelper.Decode (el.LocalName);
dataset.DataSetName = localName;
dataset.Namespace = el.NamespaceURI;
dataset.Prefix = el.Prefix;
foreach (XmlNode n in el.ChildNodes) {
if (n.NamespaceURI == XmlSchema.Namespace)
continue;
if (n.NodeType == XmlNodeType.Element)
InferTopLevelTable (n as XmlElement);
}
}
int count = 0;
foreach (TableMapping map in tables) {
string baseName = map.PrimaryKey != null ? map.PrimaryKey.ColumnName : map.Table.TableName + "_Id";
// Make sure name of RK column is unique
string rkName = baseName;
if (map.ChildTables [map.Table.TableName] != null) {
rkName = baseName + '_' + count;
while (map.GetColumn (rkName) != null) {
count++;
rkName = baseName + '_' + count;
}
}
foreach (TableMapping ct in map.ChildTables) {
ct.ReferenceKey = GetMappedColumn (ct, rkName, map.Table.Prefix, map.Table.Namespace, MappingType.Hidden, map.PrimaryKey != null ? map.PrimaryKey.DataType : typeof (int));
}
}
foreach (TableMapping map in tables) {
if (map.ExistsInDataSet)
continue;
if (map.PrimaryKey != null)
map.Table.Columns.Add (map.PrimaryKey);
foreach (DataColumn col in map.Elements)
map.Table.Columns.Add (col);
foreach (DataColumn col in map.Attributes)
map.Table.Columns.Add (col);
if (map.SimpleContent != null)
map.Table.Columns.Add (map.SimpleContent);
if (map.ReferenceKey != null)
map.Table.Columns.Add (map.ReferenceKey);
dataset.Tables.Add (map.Table);
}
foreach (RelationStructure rs in relations) {
string relName = rs.ExplicitName != null ? rs.ExplicitName : rs.ParentTableName + "_" + rs.ChildTableName;
DataTable pt = dataset.Tables [rs.ParentTableName];
DataTable ct = dataset.Tables [rs.ChildTableName];
DataColumn pc = pt.Columns [rs.ParentColumnName];
DataColumn cc = null;
// If both parent and child tables have same name, it is quite
// possible to have column names suffixed with some numbers.
if (rs.ParentTableName == rs.ChildTableName) {
cc = ct.Columns [rs.ChildColumnName + "_" + count];
}
if (cc == null)
cc = ct.Columns [rs.ChildColumnName];
if (pt == null)
throw new DataException ("Parent table was not found : " + rs.ParentTableName);
else if (ct == null)
throw new DataException ("Child table was not found : " + rs.ChildTableName);
else if (pc == null)
throw new DataException ("Parent column was not found :" + rs.ParentColumnName);
else if (cc == null)
throw new DataException ("Child column was not found :" + rs.ChildColumnName);
DataRelation rel = new DataRelation (relName, pc, cc, rs.CreateConstraint);
if (rs.IsNested) {
rel.Nested = true;
rel.ParentTable.PrimaryKey = rel.ParentColumns;
}
dataset.Relations.Add (rel);
}
}