e.Name = "bar";
s.Items.Add (e);
s.Compile (null);
+#if NET_2_0
+ schemaIntegerType = ((XmlSchemaSimpleType) a.AttributeSchemaType).Datatype;
+ schemaDecimalType = ((XmlSchemaSimpleType) b.AttributeSchemaType).Datatype;
+ schemaAnyType = e.ElementSchemaType as XmlSchemaComplexType;
+#else
schemaIntegerType = a.AttributeType as XmlSchemaDatatype;
schemaDecimalType = b.AttributeType as XmlSchemaDatatype;
schemaAnyType = e.ElementType as XmlSchemaComplexType;
+#endif
}
#region Fields
DataSet dataset;
+ bool forDataSet;
XmlSchema schema;
ArrayList relations = new ArrayList ();
// .ctor()
- public XmlSchemaDataImporter (DataSet dataset, XmlReader reader)
+ public XmlSchemaDataImporter (DataSet dataset, XmlReader reader, bool forDataSet)
{
this.dataset = dataset;
+ this.forDataSet = forDataSet;
dataset.DataSetName = "NewDataSet"; // Initialize always
schema = XmlSchema.Read (reader, null);
if (reader.NodeType == XmlNodeType.EndElement && reader.LocalName == "schema" && reader.NamespaceURI == XmlSchema.Namespace)
if (datasetElement == null &&
IsDataSetElement (el))
datasetElement = el;
+#if NET_2_0
+ if (el.ElementSchemaType is XmlSchemaComplexType &&
+ el.ElementSchemaType != schemaAnyType)
+#else
if (el.ElementType is XmlSchemaComplexType &&
-el.ElementType != schemaAnyType)
+ el.ElementType != schemaAnyType)
+#endif
targetElements.Add (obj);
}
}
foreach (XmlSchemaObject obj in schema.Items) {
if (obj is XmlSchemaElement) {
XmlSchemaElement el = obj as XmlSchemaElement;
+#if NET_2_0
+ if (el.ElementSchemaType is XmlSchemaComplexType &&
+ el.ElementSchemaType != schemaAnyType)
+#else
if (el.ElementType is XmlSchemaComplexType &&
-el.ElementType != schemaAnyType)
+ el.ElementType != schemaAnyType)
+#endif
targetElements.Add (obj);
}
}
{
XmlSchemaElement el = p as XmlSchemaElement;
if (el != null) {
- XmlSchemaComplexType ct = el.ElementType as XmlSchemaComplexType;
+ XmlSchemaComplexType ct = null;
+#if NET_2_0
+ ct = el.ElementSchemaType as XmlSchemaComplexType;
+#else
+ ct = el.ElementType as XmlSchemaComplexType;
+#endif
if (ct == null || ct == schemaAnyType)
return true; // column element
if (ct.AttributeUses.Count > 0)
return;
// If type is not complex, just skip this element
+#if NET_2_0
+ if (! (el.ElementSchemaType is XmlSchemaComplexType && el.ElementSchemaType != schemaAnyType))
+#else
if (! (el.ElementType is XmlSchemaComplexType && el.ElementType != schemaAnyType))
+#endif
return;
if (IsDataSetElement (el)) {
ProcessDataSetElement (el);
return;
}
+ else
+ dataset.Locale = CultureInfo.CurrentCulture;
// Register as a top-level element
topLevelElements.Add (el);
dataset.DataSetName = el.Name;
this.datasetElement = el;
- // Search for msdata:Locale attribute
+ // Search for locale attributes
+ bool useCurrent = false;
if (el.UnhandledAttributes != null) {
foreach (XmlAttribute attr in el.UnhandledAttributes) {
+#if NET_2_0
+ if (attr.LocalName == "UseCurrentLocale" &&
+ attr.NamespaceURI == XmlConstants.MsdataNamespace)
+ useCurrent = true;
+#endif
if (attr.LocalName == "Locale" &&
attr.NamespaceURI == XmlConstants.MsdataNamespace) {
CultureInfo ci = new CultureInfo (attr.Value);
}
}
}
+#if NET_2_0
+ if (!useCurrent && !dataset.LocaleSpecified) // then set current culture instance _explicitly_
+ dataset.Locale = CultureInfo.CurrentCulture;
+#endif
// Process content type particle (and create DataTable)
- XmlSchemaComplexType ct = el.ElementType as XmlSchemaComplexType;
+ XmlSchemaComplexType ct = null;
+#if NET_2_0
+ ct = el.ElementSchemaType as XmlSchemaComplexType;
+#else
+ ct = el.ElementType as XmlSchemaComplexType;
+#endif
XmlSchemaParticle p = ct != null ? ct.ContentTypeParticle : null;
if (p != null)
HandleDataSetContentTypeParticle (p);
{
XmlSchemaElement el = p as XmlSchemaElement;
if (el != null) {
+#if NET_2_0
+ if (el.ElementSchemaType is XmlSchemaComplexType && el.RefName != el.QualifiedName)
+#else
if (el.ElementType is XmlSchemaComplexType && el.RefName != el.QualifiedName)
+#endif
ProcessDataTableElement (el);
}
else if (p is XmlSchemaGroupBase) {
DataTable table = new DataTable (tableName);
table.Namespace = el.QualifiedName.Namespace;
+ TableStructure oldTable = currentTable;
currentTable = new TableStructure (table);
dataset.Tables.Add (table);
// Handle complex type (NOTE: It is (or should be)
// impossible the type is other than complex type).
- XmlSchemaComplexType ct = (XmlSchemaComplexType) el.ElementType;
+ XmlSchemaComplexType ct = null;
+#if NET_2_0
+ ct = (XmlSchemaComplexType) el.ElementSchemaType;
+#else
+ ct = (XmlSchemaComplexType) el.ElementType;
+#endif
// Handle attributes
foreach (DictionaryEntry de in ct.AttributeUses)
table.Columns.Add ((DataColumn) de.Value);
foreach (DataColumn dc in currentTable.NonOrdinalColumns)
table.Columns.Add (dc);
+
+ currentTable = oldTable;
}
private DataRelation GenerateRelationship (RelationStructure rs)
pcol [i] = ptab.Columns [XmlHelper.Decode (pcolnames [i])];
DataColumn[] ccol = new DataColumn [ccolnames.Length];
- for (int i=0; i < ccol.Length; ++i)
+ for (int i=0; i < ccol.Length; ++i) {
ccol [i] = ctab.Columns [XmlHelper.Decode (ccolnames [i])];
-
+ if (ccol [i] == null)
+ ccol [i] = CreateChildColumn (pcol [i], ctab);
+ }
rel = new DataRelation (name, pcol, ccol, rs.CreateConstraint);
} else {
DataColumn pcol = ptab.Columns [XmlHelper.Decode (rs.ParentColumnName)];
DataColumn ccol = ctab.Columns [XmlHelper.Decode (rs.ChildColumnName)];
- if (ccol == null) {
- ccol = new DataColumn ();
- ccol.ColumnName = pcol.ColumnName;
- ccol.Namespace = String.Empty; // don't copy
- ccol.ColumnMapping = MappingType.Hidden;
- ccol.DataType = pcol.DataType;
- ctab.Columns.Add (ccol);
- }
+ if (ccol == null)
+ ccol = CreateChildColumn (pcol, ctab);
rel = new DataRelation (name, pcol, ccol, rs.CreateConstraint);
}
rel.Nested = rs.IsNested;
return rel;
}
+ private DataColumn CreateChildColumn (DataColumn parentColumn, DataTable childTable)
+ {
+ DataColumn col = childTable.Columns.Add (parentColumn.ColumnName,
+ parentColumn.DataType);
+ col.Namespace = String.Empty;
+ col.ColumnMapping = MappingType.Hidden;
+ return col;
+ }
+
private void ImportColumnGroupBase (XmlSchemaElement parent, XmlSchemaGroupBase gb)
{
foreach (XmlSchemaParticle p in gb.Items) {
DataColumn col = new DataColumn ();
col.ColumnName = attr.QualifiedName.Name;
col.Namespace = attr.QualifiedName.Namespace;
- XmlSchemaDatatype dt = GetSchemaPrimitiveType (attr.AttributeType);
+ XmlSchemaDatatype dt = null;
+#if NET_2_0
+ dt = GetSchemaPrimitiveType (((XmlSchemaSimpleType) attr.AttributeSchemaType).Datatype);
+#else
+ dt = GetSchemaPrimitiveType (attr.AttributeType);
+#endif
// This complicated check comes from the fact that
// MS.NET fails to map System.Object to anyType (that
// will cause ReadTypedObject() fail on XmlValidatingReader).
if (attr.Use == XmlSchemaUse.Required)
col.AllowDBNull = false;
+#if NET_2_0
+ FillFacet (col, attr.AttributeSchemaType as XmlSchemaSimpleType);
+#else
FillFacet (col, attr.AttributeType as XmlSchemaSimpleType);
+#endif
// Call this method after filling the name
ImportColumnMetaInfo (attr, attr.QualifiedName, col);
col.DefaultValue = GetElementDefaultValue (el);
col.AllowDBNull = (el.MinOccurs == 0);
+#if NET_2_0
+ if (el.ElementSchemaType is XmlSchemaComplexType && el.ElementSchemaType != schemaAnyType)
+#else
if (el.ElementType is XmlSchemaComplexType && el.ElementType != schemaAnyType)
+#endif
FillDataColumnComplexElement (parent, el, col);
else if (el.MaxOccurs != 1)
FillDataColumnRepeatedSimpleElement (parent, el, col);
// common process for element and attribute
private void ImportColumnMetaInfo (XmlSchemaAnnotated obj, XmlQualifiedName name, DataColumn col)
{
- int ordinal = -1;
if (obj.UnhandledAttributes != null) {
foreach (XmlAttribute attr in obj.UnhandledAttributes) {
if (attr.NamespaceURI != XmlConstants.MsdataNamespace)
col.ReadOnly = XmlConvert.ToBoolean (attr.Value);
break;
case XmlConstants.Ordinal:
- ordinal = int.Parse (attr.Value);
+ int ordinal = int.Parse (attr.Value);
break;
}
}
if (el.Annotation != null)
HandleAnnotations (el.Annotation, true);
- // If xsd:keyref for this table exists, then don't add
+ // If xsd:keyref xsd:key for this table exists, then don't add
// relation here manually.
- else if (!DataSetDefinesPrimaryKey (elName)) {
+ else if (!DataSetDefinesKey (elName)) {
AddParentKeyColumn (parent, el, col);
- DataColumn pkey = currentTable.PrimaryKey;
RelationStructure rel = new RelationStructure ();
rel.ParentTableName = XmlHelper.Decode (parent.QualifiedName.Name);
rel.ChildTableName = elName;
- rel.ParentColumnName = pkey.ColumnName;
- rel.ChildColumnName = pkey.ColumnName;
+ rel.ParentColumnName = col.ColumnName;
+ rel.ChildColumnName = col.ColumnName;
rel.CreateConstraint = true;
rel.IsNested = true;
relations.Add (rel);
// If the element is not referenced one, the element will be handled later.
if (el.RefName == XmlQualifiedName.Empty)
- targetElements.Add (el);
+ ProcessDataTableElement (el);
}
- private bool DataSetDefinesPrimaryKey (string name)
+ private bool DataSetDefinesKey (string name)
{
foreach (ConstraintStructure c in reservedConstraints.Values)
- if (c.TableName == name && c.IsPrimaryKey)
+ if (c.TableName == name && (c.IsPrimaryKey || c.IsNested))
return true;
return false;
}
// check name identity
string name = XmlHelper.Decode (parent.QualifiedName.Name) + "_Id";
- if (currentTable.ContainsColumn (name))
- throw new DataException (String.Format ("There is already a column that has the same name: {0}", name));
+ int count = 0;
+ while (currentTable.ContainsColumn (name))
+ name = String.Format ("{0}_{1}", name, count++);
// check existing primary key
if (currentTable.Table.PrimaryKey.Length > 0)
throw new DataException (String.Format ("There is already primary key columns in the table \"{0}\".", currentTable.Table.TableName));
col.ColumnMapping = MappingType.Hidden;
col.Namespace = parent.QualifiedName.Namespace;
col.DataType = typeof (int);
- col.Unique = true;
col.AutoIncrement = true;
col.AllowDBNull = false;
cc2.Namespace = el.QualifiedName.Namespace;
cc2.ColumnMapping = MappingType.SimpleContent;
cc2.AllowDBNull = false;
+#if NET_2_0
+ cc2.DataType = ConvertDatatype (GetSchemaPrimitiveType (el.ElementSchemaType));
+#else
cc2.DataType = ConvertDatatype (GetSchemaPrimitiveType (el.ElementType));
+#endif
dt.Columns.Add (cc2);
dt.Columns.Add (cc);
col.ColumnName = XmlHelper.Decode (el.QualifiedName.Name);
col.Namespace = el.QualifiedName.Namespace;
col.ColumnMapping = MappingType.Element;
+#if NET_2_0
+ col.DataType = ConvertDatatype (GetSchemaPrimitiveType (el.ElementSchemaType));
+ FillFacet (col, el.ElementSchemaType as XmlSchemaSimpleType);
+#else
col.DataType = ConvertDatatype (GetSchemaPrimitiveType (el.ElementType));
FillFacet (col, el.ElementType as XmlSchemaSimpleType);
+#endif
ImportColumnMetaInfo (el, el.QualifiedName, col);
string tableName = c.TableName;
DataTable dt = dataset.Tables [tableName];
- if (dt == null)
- throw new DataException (String.Format ("Invalid XPath selection inside selector. Cannot find: {0}", tableName));
+ if (dt == null) {
+ if (forDataSet)
+ throw new DataException (String.Format ("Invalid XPath selection inside selector. Cannot find: {0}", tableName));
+ else
+ // nonexistent table name. .NET ignores it for DataTable.ReadXmlSchema().
+ return;
+ }
DataColumn [] cols = new DataColumn [c.Columns.Length];
for (int i = 0; i < cols.Length; i++) {
if (!c.IsConstraintOnly) {
// generate the relation.
- DataRelation rel = new DataRelation (c.ConstraintName, uniq.Columns, cols, false);
+ DataRelation rel = new DataRelation (c.ConstraintName, uniq.Columns, cols, true);
rel.Nested = c.IsNested;
rel.SetParentKeyConstraint (uniq);
rel.SetChildKeyConstraint (fkc);