New test.
[mono.git] / mcs / class / System.Data / System.Data / XmlSchemaDataImporter.cs
1 //
2 // XmlSchemaDataImporter.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // (C)2004 Novell Inc.
8 //
9 //
10 // ***** The design note became somewhat obsolete. Should be rewritten. *****
11 //
12 // * Design Notes
13 //
14 // ** Abstract
15 //
16 //      This class is used to import an XML Schema into a DataSet schema.
17 //
18 //      Only XmlReader is acceptable as the input to the class.
19 //      This class is not expected to read XML Schema multi time.
20 //
21 // ** Targetable Schema Components
22 //
23 //      Only global global elements that hold complex type are converted 
24 //      into a table. 
25 //      <del>
26 //      The components of the type of the element are subsequently converted
27 //      into a table, BUT there is an exception. As for "DataSet elements",
28 //      the type is just ignored (see "DataSet Element definition" below).
29 //      </del><ins>
30 //      The components of the type of the element are subsequently converted
31 //      into a table. As for "DataSet elements", its complex type is also 
32 //      handled.
33 //      </ins>
34 //
35 //      Unused complex types are never be converted.
36 //
37 //      Global simple types and global attributes are never converted.
38 //      They cannot be a table.
39 //      Local complex types are also converted into a table.
40 //
41 //      Local elements are converted into either a table or a column in
42 //      the "context DataTable". Simple-typed element is not always converted
43 //      into a DataColumn; if maxOccurs > 1, it will be converted as a table.
44 //
45 // ** Name Convention
46 //
47 //      Ignore this section. Microsoft.NET was buggy enough to confuse
48 //      against these name conflicts.
49 //
50 //      Since local complex types are anonymous, we have to name for each
51 //      component. Thus, and since complex types and elements can have the 
52 //      same name each other, we have to manage a table for mappings from 
53 //      a name to a component. The names must be also used in DataRelation
54 //      definitions correctly.
55 //
56 // ** DataSet element definition
57 //
58 //      "DataSet element" is 1) such element that has an attribute 
59 //      msdata:IsDataSet (where prefix "msdata" is bound to 
60 //      urn:schemas-microsoft-com:xml-msdata), or 2) the only one
61 //      element definition in the schema.
62 //
63 //      There is another complicated rule. 1) If there is only one element EL 
64 //      in the schema, and 2) if the type of EL is complex named CT, and 3)
65 //      the content of the CT is a group base, and 4) the group base contains 
66 //      an element EL2, and finally 5) if EL2 is complex, THEN the element is
67 //      the DataSet element.
68 //
69 //      Only the first global element that matches the condition above is
70 //      regarded as DataSet element (by necessary design or just a bug?) 
71 //      instead of handling as an error.
72 //
73 //      All global elements are considered as an alternative in the dataset
74 //      element.
75 //
76 //      For local elements, msdata:IsDataSet are just ignored.
77 //
78 // ** Importing Complex Types as Columns
79 //
80 //      When an xs:element is going to be mapped, its complex type (remember
81 //      that only complex-typed elements are targettable) are expanded to
82 //      DataColumn.
83 //
84 //      DataColumn has a property MappingType that shows whether this column
85 //       came from attribute or element.
86 //
87 //      [Question: How about MappingType.Simple? How is it used?]
88 //
89 //      Additionally, for particle elements, it might also create another
90 //      DataTable (but for the particle elements in context DataTable, it
91 //      will create an index to the new table).
92 //
93 //      For group base particles (XmlSchemaGroupBase; sequence, choice, all)
94 //      each component in those groups are mapped to a column. Even if you
95 //      import "choice" or "all" components, DataSet.WriteXmlSchema() will
96 //      output them just as a "sequence".
97 //
98 //      Columns cannot be added directly to current context DataTable; they
99 //      need to be added after processing all the columns, because they may
100 //      have msdata:Ordinal attribute that specifies the order of the columns
101 //      in the DataTable.
102 //
103 //      "Nested elements" are not allowed. (Clarification required?)
104 //
105 // ** Identity Constraints and DataRelations
106 //
107 // *** DataRelations from element identity constraints
108 //
109 //      Only constraints on "DataSet element" is considered. All other
110 //      constraint definitions are ignored. Note that it is DataSet that has
111 //      the property Relations (of type DataRelationCollection).
112 //
113 //      xs:key and xs:unique are handled as the same (then both will be
114 //      serialized as xs:unique).
115 //
116 //      The XPath expressions in the constraints are strictly limited; they
117 //      are expected to be expandable enough to be mappable for each
118 //
119 //              * selector to "any_valid_XPath/is/OK/blah" 
120 //                where "blah" is one of the DataTable name. It looks that
121 //                only the last QName section is significant and any heading
122 //                XPath step is OK (even if the mapped node does not exist).
123 //              * field to QName that is mapped to DataColumn in the DataTable
124 //                (even ./QName is not allowed)
125 //
126 // *** DataRelations from annotations
127 //
128 //      See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/_mapping_relationship_specified_for_nested_elements.asp and http://msdn.microsoft.com/library/en-us/cpguide/html/_specifying_relationship_between_elements_with_no_nesting.asp
129 //
130 // ** Informative References
131 //
132 // Generating DataSet Relational Structure from XML Schema (XSD)
133 // http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/_generating_dataset_relational_structure_from_xsd.asp
134 //
135
136 //
137 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
138 //
139 // Permission is hereby granted, free of charge, to any person obtaining
140 // a copy of this software and associated documentation files (the
141 // "Software"), to deal in the Software without restriction, including
142 // without limitation the rights to use, copy, modify, merge, publish,
143 // distribute, sublicense, and/or sell copies of the Software, and to
144 // permit persons to whom the Software is furnished to do so, subject to
145 // the following conditions:
146 // 
147 // The above copyright notice and this permission notice shall be
148 // included in all copies or substantial portions of the Software.
149 // 
150 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
151 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
152 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
153 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
154 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
155 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
156 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
157 //
158
159 using System;
160 using System.Collections;
161 using System.Data;
162 using System.Globalization;
163 using System.Xml;
164 using System.Xml.Schema;
165
166
167 namespace System.Data
168 {
169         internal class TableStructureCollection : CollectionBase
170         {
171                 public void Add (TableStructure table)
172                 {
173                         List.Add (table);
174                 }
175
176                 public TableStructure this [int i] {
177                         get { return List [i] as TableStructure; }
178                 }
179
180                 public TableStructure this [string name] {
181                         get {
182                                 foreach (TableStructure ts in List)
183                                         if (ts.Table.TableName == name)
184                                                 return ts;
185                                 return null;
186                         }
187                 }
188         }
189
190         internal class RelationStructureCollection : CollectionBase
191         {
192                 public void Add (RelationStructure rel)
193                 {
194                         List.Add (rel);
195                 }
196
197                 public RelationStructure this [int i] {
198                         get { return List [i] as RelationStructure; }
199                 }
200
201                 public RelationStructure this [string parent, string child] {
202                         get {
203                                 foreach (RelationStructure rel in List)
204                                         if (rel.ParentTableName == parent && rel.ChildTableName == child)
205                                                 return rel;
206                                 return null;
207                         }
208                 }
209         }
210
211         internal class TableStructure
212         {
213                 public TableStructure (DataTable table)
214                 {
215                         this.Table = table;
216                 }
217
218                 // The columns and orders which will be added to the context
219                 // table (See design notes; Because of the ordinal problem)
220                 public DataTable Table;
221                 public Hashtable OrdinalColumns = new Hashtable ();
222                 public ArrayList NonOrdinalColumns = new ArrayList ();
223                 public DataColumn PrimaryKey;
224
225                 public bool ContainsColumn (string name)
226                 {
227                         foreach (DataColumn col in NonOrdinalColumns)
228                                 if (col.ColumnName == name)
229                                         return true;
230                         foreach (DataColumn col in OrdinalColumns.Keys)
231                                 if (col.ColumnName == name)
232                                         return true;
233                         return false;
234                 }
235         }
236
237         internal class RelationStructure
238         {
239                 public string ExplicitName;
240                 public string ParentTableName;
241                 public string ChildTableName;
242                 public string ParentColumnName;
243                 public string ChildColumnName;
244                 public bool IsNested;
245                 public bool CreateConstraint;
246         }
247
248         internal class ConstraintStructure
249         {
250                 public readonly string TableName;
251                 public readonly string [] Columns;
252                 public readonly bool [] IsAttribute;
253                 public readonly string ConstraintName;
254                 public readonly bool IsPrimaryKey;
255                 public readonly string ReferName;
256                 public readonly bool IsNested;
257                 public readonly bool IsConstraintOnly;
258
259                 public ConstraintStructure (string tname, string [] cols, bool [] isAttr, string cname, bool isPK, string refName, bool isNested, bool isConstraintOnly)
260                 {
261                         TableName = tname;
262                         Columns = cols;
263                         IsAttribute = isAttr;
264                         ConstraintName = XmlHelper.Decode (cname);
265                         IsPrimaryKey = isPK;
266                         ReferName = refName;
267                         IsNested = isNested;
268                         IsConstraintOnly = isConstraintOnly;
269                 }
270         }
271
272         internal class XmlSchemaDataImporter
273         {
274                 static readonly XmlSchemaDatatype schemaIntegerType;
275                 static readonly XmlSchemaDatatype schemaDecimalType;
276                 static readonly XmlSchemaComplexType schemaAnyType;
277
278                 static XmlSchemaDataImporter ()
279                 {
280                         XmlSchema s = new XmlSchema ();
281                         XmlSchemaAttribute a = new XmlSchemaAttribute ();
282                         a.Name = "foo";
283                         a.SchemaTypeName = new XmlQualifiedName ("integer", XmlSchema.Namespace);
284                         s.Items.Add (a);
285                         XmlSchemaAttribute b = new XmlSchemaAttribute ();
286                         b.Name = "bar";
287                         b.SchemaTypeName = new XmlQualifiedName ("decimal", XmlSchema.Namespace);
288                         s.Items.Add (b);
289                         XmlSchemaElement e = new XmlSchemaElement ();
290                         e.Name = "bar";
291                         s.Items.Add (e);
292                         s.Compile (null);
293 #if NET_2_0
294                         schemaIntegerType = ((XmlSchemaSimpleType) a.AttributeSchemaType).Datatype;
295                         schemaDecimalType = ((XmlSchemaSimpleType) b.AttributeSchemaType).Datatype;
296                         schemaAnyType = e.ElementSchemaType as XmlSchemaComplexType;
297 #else
298                         schemaIntegerType = a.AttributeType as XmlSchemaDatatype;
299                         schemaDecimalType = b.AttributeType as XmlSchemaDatatype;
300                         schemaAnyType = e.ElementType as XmlSchemaComplexType;
301 #endif
302                 }
303
304                 #region Fields
305
306                 DataSet dataset;
307                 XmlSchema schema;
308
309                 ArrayList relations = new ArrayList ();
310                 Hashtable reservedConstraints = new Hashtable ();
311
312                 // such element that has an attribute msdata:IsDataSet="true"
313                 XmlSchemaElement datasetElement;
314
315                 // choice alternatives in the "dataset element"
316                 ArrayList topLevelElements = new ArrayList ();
317
318                 // import target elements
319                 ArrayList targetElements = new ArrayList ();
320
321                 TableStructure currentTable;
322
323                 #endregion
324
325                 // .ctor()
326
327                 public XmlSchemaDataImporter (DataSet dataset, XmlReader reader)
328                 {
329                         this.dataset = dataset;
330                         dataset.DataSetName = "NewDataSet"; // Initialize always
331                         schema = XmlSchema.Read (reader, null);
332                         if (reader.NodeType == XmlNodeType.EndElement && reader.LocalName == "schema" && reader.NamespaceURI == XmlSchema.Namespace)
333                                 reader.ReadEndElement ();
334                         schema.Compile (null);
335                 }
336
337                 // methods
338
339                 public void Process ()
340                 {
341                         if (schema.Id != null)
342                                 dataset.DataSetName = schema.Id; // default. Overridable by "DataSet element"
343                         dataset.Namespace = schema.TargetNamespace;
344
345                         // Find dataset element
346                         foreach (XmlSchemaObject obj in schema.Items) {
347                                 XmlSchemaElement el = obj as XmlSchemaElement;
348                                 if (el != null) {
349                                         if (datasetElement == null &&
350                                                 IsDataSetElement (el))
351                                                 datasetElement = el;
352 #if NET_2_0
353                                         if (el.ElementSchemaType is XmlSchemaComplexType &&
354                                             el.ElementSchemaType != schemaAnyType)
355 #else
356                                         if (el.ElementType is XmlSchemaComplexType &&
357                                             el.ElementType != schemaAnyType)
358 #endif
359                                                 targetElements.Add (obj);
360                                 }
361                         }
362
363                         // make reservation of identity constraints
364                         if (datasetElement != null) {
365                                 // keys/uniques.
366                                 foreach (XmlSchemaObject obj in datasetElement.Constraints)
367                                         if (! (obj is XmlSchemaKeyref))
368                                                 ReserveSelfIdentity ((XmlSchemaIdentityConstraint) obj);
369                                 // keyrefs.
370                                 foreach (XmlSchemaObject obj in datasetElement.Constraints)
371                                         if (obj is XmlSchemaKeyref)
372                                                 ReserveRelationIdentity (datasetElement, (XmlSchemaKeyref) obj);
373                         }
374
375                         foreach (XmlSchemaObject obj in schema.Items) {
376                                 if (obj is XmlSchemaElement) {
377                                         XmlSchemaElement el = obj as XmlSchemaElement;
378 #if NET_2_0
379                                         if (el.ElementSchemaType is XmlSchemaComplexType &&
380                                             el.ElementSchemaType != schemaAnyType)
381 #else
382                                         if (el.ElementType is XmlSchemaComplexType &&
383                                             el.ElementType != schemaAnyType)
384 #endif
385                                                 targetElements.Add (obj);
386                                 }
387                         }
388
389                         // This collection will grow up while processing elements.
390                         int globalElementCount = targetElements.Count;
391
392                         for (int i = 0; i < globalElementCount; i++)
393                                 ProcessGlobalElement ((XmlSchemaElement) targetElements [i]);
394
395                         // Rest are local elements.
396                         for (int i = globalElementCount; i < targetElements.Count; i++)
397                                 ProcessDataTableElement ((XmlSchemaElement) targetElements [i]);
398
399                         // Handle relation definitions written as xs:annotation.
400                         // See detail: http://msdn.microsoft.com/library/shared/happyUrl/fnf_msdn.asp?Redirect=%22http://msdn.microsoft.com/404/default.asp%22
401                         foreach (XmlSchemaObject obj in schema.Items)
402                                 if (obj is XmlSchemaAnnotation)
403                                         HandleAnnotations ((XmlSchemaAnnotation) obj, false);
404
405                         if (datasetElement != null) {
406                                 // Handle constraints in the DataSet element. First keys.
407                                 foreach (XmlSchemaObject obj in datasetElement.Constraints)
408                                         if (! (obj is XmlSchemaKeyref))
409                                                 ProcessSelfIdentity (reservedConstraints [obj] as ConstraintStructure);
410                                 // Then keyrefs.
411                                 foreach (XmlSchemaObject obj in datasetElement.Constraints)
412                                         if (obj is XmlSchemaKeyref)
413                                                 ProcessRelationIdentity (datasetElement, reservedConstraints [obj] as ConstraintStructure);
414                         }
415
416                         foreach (RelationStructure rs in this.relations)
417                                 dataset.Relations.Add (GenerateRelationship (rs));
418                 }
419
420                 private bool IsDataSetElement (XmlSchemaElement el)
421                 {
422                         if (el.UnhandledAttributes != null) {
423                                 foreach (XmlAttribute attr in el.UnhandledAttributes) {
424                                         if (attr.LocalName == "IsDataSet" &&
425                                                 attr.NamespaceURI == XmlConstants.MsdataNamespace) {
426                                                 switch (attr.Value) {
427                                                 case "true": // case sensitive
428                                                         return true;
429                                                 case "false":
430                                                         break;
431                                                 default:
432                                                         throw new DataException (String.Format ("Value {0} is invalid for attribute 'IsDataSet'.", attr.Value));
433                                                 }
434                                         }
435                                 }
436                         }
437
438                         if (schema.Elements.Count != 1)
439                                 return false;
440                         if (!(el.SchemaType is XmlSchemaComplexType))
441                                 return false;
442                         XmlSchemaComplexType ct = (XmlSchemaComplexType) el.SchemaType;
443                         if (ct.AttributeUses.Count > 0)
444                                 return false;
445                         XmlSchemaGroupBase gb = ct.ContentTypeParticle as XmlSchemaGroupBase;
446                         if (gb == null || gb.Items.Count == 0)
447                                 return false;
448                         foreach (XmlSchemaParticle p in gb.Items) {
449                                 if (ContainsColumn (p))
450                                         return false;
451                         }
452                         return true;
453                 }
454
455                 private bool ContainsColumn (XmlSchemaParticle p)
456                 {
457                         XmlSchemaElement el = p as XmlSchemaElement;
458                         if (el != null) {
459                                 XmlSchemaComplexType ct = null;
460 #if NET_2_0
461                                 ct = el.ElementSchemaType as XmlSchemaComplexType;
462 #else
463                                 ct = el.ElementType as XmlSchemaComplexType;
464 #endif
465                                 if (ct == null || ct == schemaAnyType)
466                                         return true; // column element
467                                 if (ct.AttributeUses.Count > 0)
468                                         return false; // table element
469                                 if (ct.ContentType == XmlSchemaContentType.TextOnly)
470                                         return true; // column element
471                                 else
472                                         return false; // table element
473                         }
474                         XmlSchemaGroupBase gb = p as XmlSchemaGroupBase;
475                         for (int i = 0; i < gb.Items.Count; i++) {
476                                 if (ContainsColumn ((XmlSchemaParticle) gb.Items [i]))
477                                         return true;
478                         }
479                         return false;
480                 }
481
482                 private void ProcessGlobalElement (XmlSchemaElement el)
483                 {
484                         // If it is already registered (by resolving reference
485                         // in previously-imported elements), just ignore.
486                         if (dataset.Tables.Contains (el.QualifiedName.Name))
487                                 return;
488
489                         // If type is not complex, just skip this element
490 #if NET_2_0
491                         if (! (el.ElementSchemaType is XmlSchemaComplexType && el.ElementSchemaType != schemaAnyType))
492 #else
493                         if (! (el.ElementType is XmlSchemaComplexType && el.ElementType != schemaAnyType))
494 #endif
495                                 return;
496
497                         if (IsDataSetElement (el)) {
498                                 ProcessDataSetElement (el);
499                                 return;
500                         }
501
502                         // Register as a top-level element
503                         topLevelElements.Add (el);
504                         // Create DataTable for this element
505                         ProcessDataTableElement (el);
506                 }
507
508                 private void ProcessDataSetElement (XmlSchemaElement el)
509                 {
510                         dataset.DataSetName = el.Name;
511                         this.datasetElement = el;
512
513                         // Search for msdata:Locale attribute
514                         if (el.UnhandledAttributes != null) {
515                                 foreach (XmlAttribute attr in el.UnhandledAttributes) {
516                                         if (attr.LocalName == "Locale" &&
517                                                 attr.NamespaceURI == XmlConstants.MsdataNamespace) {
518                                                 CultureInfo ci = new CultureInfo (attr.Value);
519                                                 dataset.Locale = ci;
520                                         }
521                                 }
522                         }
523
524                         // Process content type particle (and create DataTable)
525                         XmlSchemaComplexType ct = null;
526 #if NET_2_0
527                         ct = el.ElementSchemaType as XmlSchemaComplexType;
528 #else
529                         ct = el.ElementType as XmlSchemaComplexType;
530 #endif
531                         XmlSchemaParticle p = ct != null ? ct.ContentTypeParticle : null;
532                         if (p != null)
533                                 HandleDataSetContentTypeParticle (p);
534                 }
535
536                 private void HandleDataSetContentTypeParticle (XmlSchemaParticle p)
537                 {
538                         XmlSchemaElement el = p as XmlSchemaElement;
539                         if (el != null) {
540 #if NET_2_0
541                                 if (el.ElementSchemaType is XmlSchemaComplexType && el.RefName != el.QualifiedName)
542 #else
543                                 if (el.ElementType is XmlSchemaComplexType && el.RefName != el.QualifiedName)
544 #endif
545                                         ProcessDataTableElement (el);
546                         }
547                         else if (p is XmlSchemaGroupBase) {
548                                 foreach (XmlSchemaParticle pc in ((XmlSchemaGroupBase) p).Items)
549                                         HandleDataSetContentTypeParticle (pc);
550                         }
551                 }
552
553                 private void ProcessDataTableElement (XmlSchemaElement el)
554                 {
555                         string tableName = XmlHelper.Decode (el.QualifiedName.Name);
556                         // If it is already registered, just ignore.
557                         if (dataset.Tables.Contains (tableName))
558                                 return;
559
560                         DataTable table = new DataTable (tableName);
561                         table.Namespace = el.QualifiedName.Namespace;
562                         currentTable = new TableStructure (table);
563
564                         dataset.Tables.Add (table);
565
566                         // Find Locale
567                         if (el.UnhandledAttributes != null) {
568                                 foreach (XmlAttribute attr in el.UnhandledAttributes) {
569                                         if (attr.LocalName == "Locale" &&
570                                                 attr.NamespaceURI == XmlConstants.MsdataNamespace)
571                                                 table.Locale = new CultureInfo (attr.Value);
572                                 }
573                         }
574
575                         // Handle complex type (NOTE: It is (or should be)
576                         // impossible the type is other than complex type).
577                         XmlSchemaComplexType ct = null;
578 #if NET_2_0
579                         ct = (XmlSchemaComplexType) el.ElementSchemaType;
580 #else
581                         ct = (XmlSchemaComplexType) el.ElementType;
582 #endif
583
584                         // Handle attributes
585                         foreach (DictionaryEntry de in ct.AttributeUses)
586                                 ImportColumnAttribute ((XmlSchemaAttribute) de.Value);
587
588                         // Handle content type particle
589                         if (ct.ContentTypeParticle is XmlSchemaElement)
590                                 ImportColumnElement (el, (XmlSchemaElement) ct.ContentTypeParticle);
591                         else if (ct.ContentTypeParticle is XmlSchemaGroupBase)
592                                 ImportColumnGroupBase (el, (XmlSchemaGroupBase) ct.ContentTypeParticle);
593                         // else if null then do nothing.
594
595                         // Handle simple content
596                         switch (ct.ContentType) {
597                         case XmlSchemaContentType.TextOnly:
598 //                      case XmlSchemaContentType.Mixed:
599                                 // LAMESPEC: When reading from XML Schema, it maps to "_text", while on the data inference, it is mapped to "_Text" (case ignorant).
600                                 string simpleName = el.QualifiedName.Name + "_text";
601                                 DataColumn simple = new DataColumn (simpleName);
602                                 simple.Namespace = el.QualifiedName.Namespace;
603                                 simple.AllowDBNull = (el.MinOccurs == 0);
604                                 simple.ColumnMapping = MappingType.SimpleContent;
605                                 simple.DataType = ConvertDatatype (ct.Datatype);
606                                 currentTable.NonOrdinalColumns.Add (simple);
607                                 break;
608                         }
609
610                         // add columns to the table in specified order 
611                         // (by msdata:Ordinal attributes)
612                         SortedList sd = new SortedList ();
613                         foreach (DictionaryEntry de in currentTable.OrdinalColumns)
614                                 sd.Add (de.Value, de.Key);
615                         foreach (DictionaryEntry de in sd)
616                                 table.Columns.Add ((DataColumn) de.Value);
617                         foreach (DataColumn dc in currentTable.NonOrdinalColumns)
618                                 table.Columns.Add (dc);
619                 }
620
621                 private DataRelation GenerateRelationship (RelationStructure rs)
622                 {
623                         DataTable ptab = dataset.Tables [rs.ParentTableName];
624                         DataTable ctab = dataset.Tables [rs.ChildTableName];
625
626                         DataRelation rel ;
627                         string name = rs.ExplicitName != null ? rs.ExplicitName : XmlHelper.Decode (ptab.TableName) + '_' + XmlHelper.Decode (ctab.TableName);
628
629                         // Annotation Relations belonging to a DataSet can contain multiple colnames
630                         // in parentkey and childkey.
631                         if (datasetElement != null) {
632                                 String[] pcolnames = rs.ParentColumnName.Split (null);
633                                 String[] ccolnames = rs.ChildColumnName.Split (null);
634
635                                 DataColumn[] pcol = new DataColumn [pcolnames.Length];
636                                 for (int i=0; i<pcol.Length; ++i)
637                                         pcol [i] = ptab.Columns [XmlHelper.Decode (pcolnames [i])];
638
639                                 DataColumn[] ccol = new DataColumn [ccolnames.Length];
640                                 for (int i=0; i < ccol.Length; ++i) {
641                                         ccol [i] = ctab.Columns [XmlHelper.Decode (ccolnames [i])];
642                                         if (ccol [i] == null)
643                                                 ccol [i] = CreateChildColumn (pcol [i], ctab);
644                                 }
645                                 rel = new DataRelation (name, pcol, ccol, rs.CreateConstraint);
646                         } else {
647                                 DataColumn pcol = ptab.Columns [XmlHelper.Decode (rs.ParentColumnName)];
648                                 DataColumn ccol = ctab.Columns [XmlHelper.Decode (rs.ChildColumnName)];
649                                 if (ccol == null) 
650                                         ccol = CreateChildColumn (pcol, ctab);
651                                 rel = new DataRelation (name, pcol, ccol, rs.CreateConstraint);
652                         }
653                         rel.Nested = rs.IsNested;
654                         if (rs.CreateConstraint)
655                                 rel.ParentTable.PrimaryKey = rel.ParentColumns;
656                         return rel;
657                 }
658
659                 private DataColumn CreateChildColumn (DataColumn parentColumn, DataTable childTable)
660                 {
661                         DataColumn col = childTable.Columns.Add (parentColumn.ColumnName, 
662                                                                 parentColumn.DataType);
663                         col.Namespace = String.Empty;
664                         col.ColumnMapping = MappingType.Hidden;
665                         return col;
666                 }
667
668                 private void ImportColumnGroupBase (XmlSchemaElement parent, XmlSchemaGroupBase gb)
669                 {
670                         foreach (XmlSchemaParticle p in gb.Items) {
671                                 XmlSchemaElement el = p as XmlSchemaElement;
672                                 if (el != null)
673                                         ImportColumnElement (parent, el);
674                                 else if (p is XmlSchemaGroupBase)
675                                         ImportColumnGroupBase (parent, (XmlSchemaGroupBase) p);
676                                 // otherwise p is xs:any
677                         }
678                 }
679
680                 private XmlSchemaDatatype GetSchemaPrimitiveType (object type)
681                 {
682                         if (type is XmlSchemaComplexType)
683                                 return null; // It came here, so that maybe it is xs:anyType
684                         XmlSchemaDatatype dt = type as XmlSchemaDatatype;
685                         if (dt == null && type != null)
686                                 dt = ((XmlSchemaSimpleType) type).Datatype;
687                         return dt;
688                 }
689
690                 // Note that this column might be Hidden
691                 private void ImportColumnAttribute (XmlSchemaAttribute attr)
692                 {
693                         DataColumn col = new DataColumn ();
694                         col.ColumnName = attr.QualifiedName.Name;
695                         col.Namespace = attr.QualifiedName.Namespace;
696                         XmlSchemaDatatype dt = null;
697 #if NET_2_0
698                         dt = GetSchemaPrimitiveType (((XmlSchemaSimpleType) attr.AttributeSchemaType).Datatype);
699 #else
700                         dt = GetSchemaPrimitiveType (attr.AttributeType);
701 #endif
702                         // This complicated check comes from the fact that
703                         // MS.NET fails to map System.Object to anyType (that
704                         // will cause ReadTypedObject() fail on XmlValidatingReader).
705                         // ONLY In DataSet context, we set System.String for
706                         // simple ur-type.
707                         col.DataType = ConvertDatatype (dt);
708                         if (col.DataType == typeof (object))
709                                 col.DataType = typeof (string);
710                         // When attribute use="prohibited", then it is regarded as 
711                         // Hidden column.
712                         if (attr.Use == XmlSchemaUse.Prohibited)
713                                 col.ColumnMapping = MappingType.Hidden;
714                         else {
715                                 col.ColumnMapping = MappingType.Attribute;
716                                 col.DefaultValue = GetAttributeDefaultValue (attr);
717                         }
718                         if (attr.Use == XmlSchemaUse.Required)
719                                 col.AllowDBNull = false;
720
721 #if NET_2_0
722                         FillFacet (col, attr.AttributeSchemaType as XmlSchemaSimpleType);
723 #else
724                         FillFacet (col, attr.AttributeType as XmlSchemaSimpleType);
725 #endif
726
727                         // Call this method after filling the name
728                         ImportColumnMetaInfo (attr, attr.QualifiedName, col);
729                         AddColumn (col);
730                 }
731
732                 private void ImportColumnElement (XmlSchemaElement parent, XmlSchemaElement el)
733                 {
734                         // FIXME: element nest check
735
736                         DataColumn col = new DataColumn ();
737                         col.DefaultValue = GetElementDefaultValue (el);
738                         col.AllowDBNull = (el.MinOccurs == 0);
739
740 #if NET_2_0
741                         if (el.ElementSchemaType is XmlSchemaComplexType && el.ElementSchemaType != schemaAnyType)
742 #else
743                         if (el.ElementType is XmlSchemaComplexType && el.ElementType != schemaAnyType)
744 #endif
745                                 FillDataColumnComplexElement (parent, el, col);
746                         else if (el.MaxOccurs != 1)
747                                 FillDataColumnRepeatedSimpleElement (parent, el, col);
748                         else
749                                 FillDataColumnSimpleElement (el, col);
750                 }
751
752                 // common process for element and attribute
753                 private void ImportColumnMetaInfo (XmlSchemaAnnotated obj, XmlQualifiedName name, DataColumn col)
754                 {
755                         int ordinal = -1;
756                         if (obj.UnhandledAttributes != null) {
757                                 foreach (XmlAttribute attr in obj.UnhandledAttributes) {
758                                         if (attr.NamespaceURI != XmlConstants.MsdataNamespace)
759                                                 continue;
760                                         switch (attr.LocalName) {
761                                         case XmlConstants.Caption:
762                                                 col.Caption = attr.Value;
763                                                 break;
764                                         case XmlConstants.DataType:
765                                                 col.DataType = Type.GetType (attr.Value);
766                                                 break;
767                                         case XmlConstants.AutoIncrement:
768                                                 col.AutoIncrement = bool.Parse (attr.Value);
769                                                 break;
770                                         case XmlConstants.AutoIncrementSeed:
771                                                 col.AutoIncrementSeed = int.Parse (attr.Value);
772                                                 break;
773                                         case XmlConstants.AutoIncrementStep:
774                                                 col.AutoIncrementStep = int.Parse (attr.Value);
775                                                 break;
776                                         case XmlConstants.ReadOnly:
777                                                 col.ReadOnly = XmlConvert.ToBoolean (attr.Value);
778                                                 break;
779                                         case XmlConstants.Ordinal:
780                                                 ordinal = int.Parse (attr.Value);
781                                                 break;
782                                         }
783                                 }
784                         }
785                 }
786
787                 private void FillDataColumnComplexElement (XmlSchemaElement parent, XmlSchemaElement el, DataColumn col)
788                 {
789                         if (targetElements.Contains (el))
790                                 return; // do nothing
791
792                         string elName = XmlHelper.Decode (el.QualifiedName.Name);
793                         if (elName == dataset.DataSetName)
794                                 // Well, why it is ArgumentException :-?
795                                 throw new ArgumentException ("Nested element must not have the same name as DataSet's name.");
796
797                         if (el.Annotation != null)
798                                 HandleAnnotations (el.Annotation, true);
799                         // If xsd:keyref xsd:key for this table exists, then don't add
800                         // relation here manually.
801                         else if (!DataSetDefinesKey (elName)) {
802                                 AddParentKeyColumn (parent, el, col);
803
804                                 RelationStructure rel = new RelationStructure ();
805                                 rel.ParentTableName = XmlHelper.Decode (parent.QualifiedName.Name);
806                                 rel.ChildTableName = elName;
807                                 rel.ParentColumnName = col.ColumnName;
808                                 rel.ChildColumnName = col.ColumnName;
809                                 rel.CreateConstraint = true;
810                                 rel.IsNested = true;
811                                 relations.Add (rel);
812                         }
813
814                         // If the element is not referenced one, the element will be handled later.
815                         if (el.RefName == XmlQualifiedName.Empty)
816                                 targetElements.Add (el);
817
818                 }
819
820                 private bool DataSetDefinesKey (string name)
821                 {
822                         foreach (ConstraintStructure c in reservedConstraints.Values)
823                                 if (c.TableName == name && (c.IsPrimaryKey || c.IsNested))
824                                         return true;
825                         return false;
826                 }
827
828                 private void AddParentKeyColumn (XmlSchemaElement parent, XmlSchemaElement el, DataColumn col)
829                 {
830                         if (currentTable.PrimaryKey != null)
831                                 return;
832
833                         // check name identity
834                         string name = XmlHelper.Decode (parent.QualifiedName.Name) + "_Id";
835                         int count = 0;
836                         while (currentTable.ContainsColumn (name))
837                                 name = String.Format ("{0}_{1}", name, count++);
838                         // check existing primary key
839                         if (currentTable.Table.PrimaryKey.Length > 0)
840                                 throw new DataException (String.Format ("There is already primary key columns in the table \"{0}\".", currentTable.Table.TableName));
841
842                         col.ColumnName = name;
843                         col.ColumnMapping = MappingType.Hidden;
844                         col.Namespace = parent.QualifiedName.Namespace;
845                         col.DataType = typeof (int);
846                         col.AutoIncrement = true;
847                         col.AllowDBNull = false;
848
849                         ImportColumnMetaInfo (el, el.QualifiedName, col);
850                         AddColumn (col);
851                         currentTable.PrimaryKey = col;
852                 }
853
854                 private void FillDataColumnRepeatedSimpleElement (XmlSchemaElement parent, XmlSchemaElement el, DataColumn col)
855                 {
856                         if (targetElements.Contains (el))
857                                 return; // do nothing
858
859                         AddParentKeyColumn (parent, el, col);
860                         DataColumn pkey = currentTable.PrimaryKey;
861
862                         string elName = XmlHelper.Decode (el.QualifiedName.Name);
863                         string parentName = XmlHelper.Decode (parent.QualifiedName.Name);
864
865                         DataTable dt = new DataTable ();
866                         dt.TableName = elName;
867                         dt.Namespace = el.QualifiedName.Namespace;
868                         // reference key column to parent
869                         DataColumn cc = new DataColumn ();
870                         cc.ColumnName = parentName + "_Id";
871                         cc.Namespace = parent.QualifiedName.Namespace;
872                         cc.ColumnMapping = MappingType.Hidden;
873                         cc.DataType = typeof (int);
874
875                         // repeatable content simple element
876                         DataColumn cc2 = new DataColumn ();
877                         cc2.ColumnName = elName + "_Column";
878                         cc2.Namespace = el.QualifiedName.Namespace;
879                         cc2.ColumnMapping = MappingType.SimpleContent;
880                         cc2.AllowDBNull = false;
881 #if NET_2_0
882                         cc2.DataType = ConvertDatatype (GetSchemaPrimitiveType (el.ElementSchemaType));
883 #else
884                         cc2.DataType = ConvertDatatype (GetSchemaPrimitiveType (el.ElementType));
885 #endif
886
887                         dt.Columns.Add (cc2);
888                         dt.Columns.Add (cc);
889                         dataset.Tables.Add (dt);
890
891                         RelationStructure rel = new RelationStructure ();
892                         rel.ParentTableName = parentName;
893                         rel.ChildTableName = dt.TableName;
894                         rel.ParentColumnName = pkey.ColumnName;
895                         rel.ChildColumnName = cc.ColumnName;
896                         rel.IsNested = true;
897                         rel.CreateConstraint = true;
898                         relations.Add (rel);
899                 }
900
901                 private void FillDataColumnSimpleElement (XmlSchemaElement el, DataColumn col)
902                 {
903                         col.ColumnName = XmlHelper.Decode (el.QualifiedName.Name);
904                         col.Namespace = el.QualifiedName.Namespace;
905                         col.ColumnMapping = MappingType.Element;
906 #if NET_2_0
907                         col.DataType = ConvertDatatype (GetSchemaPrimitiveType (el.ElementSchemaType));
908                         FillFacet (col, el.ElementSchemaType as XmlSchemaSimpleType);
909 #else
910                         col.DataType = ConvertDatatype (GetSchemaPrimitiveType (el.ElementType));
911                         FillFacet (col, el.ElementType as XmlSchemaSimpleType);
912 #endif
913
914                         ImportColumnMetaInfo (el, el.QualifiedName, col);
915
916                         AddColumn (col);
917                 }
918
919                 private void AddColumn (DataColumn col)
920                 {
921                         if (col.Ordinal < 0)
922                                 currentTable.NonOrdinalColumns.Add (col);
923                         else
924                                 currentTable.OrdinalColumns.Add (col, col.Ordinal);
925                 }
926
927                 private void FillFacet (DataColumn col, XmlSchemaSimpleType st)
928                 {
929                         if (st == null || st.Content == null)
930                                 return;
931
932                         // Handle restriction facets
933
934                         XmlSchemaSimpleTypeRestriction restriction = st == null ? null : st.Content as XmlSchemaSimpleTypeRestriction;
935                         if (restriction == null)
936                                 throw new DataException ("DataSet does not suport 'list' nor 'union' simple type.");
937
938                         foreach (XmlSchemaFacet f in restriction.Facets) {
939                                 if (f is XmlSchemaMaxLengthFacet)
940                                         // There is no reason why MaxLength is limited to int, except for the fact that DataColumn.MaxLength property is int.
941                                         col.MaxLength = int.Parse (f.Value);
942                         }
943                 }
944
945                 private Type ConvertDatatype (XmlSchemaDatatype dt)
946                 {
947                         if (dt == null)
948                                 return typeof (string);
949                         else if (dt.ValueType == typeof (decimal)) {
950                                 // LAMESPEC: MSDN documentation says it is based 
951                                 // on ValueType. However, in the System.Xml.Schema
952                                 // context, xs:integer is mapped to Decimal, while
953                                 // in DataSet context it is mapped to Int64.
954                                 if (dt == schemaDecimalType)
955                                         return typeof (decimal);
956                                 else if (dt == schemaIntegerType)
957                                         return typeof (long);
958                                 else
959                                         return typeof (ulong);
960                         }
961                         else
962                                 return dt.ValueType;
963                 }
964
965                 // This method cuts out the local name of the last step from XPath.
966                 // It is nothing more than hack. However, MS looks to do similar.
967                 private string GetSelectorTarget (string xpath)
968                 {
969                         string tableName = xpath;
970                         int index = tableName.LastIndexOf ('/');
971                         // '>' is enough. If XPath [0] = '/', it is invalid. 
972                         // Selector can specify only element axes.
973                         if (index > 0)
974                                 tableName = tableName.Substring (index + 1);
975
976                         // Round QName to NSName
977                         index = tableName.LastIndexOf (':');
978                         if (index > 0)
979                                 tableName = tableName.Substring (index + 1);
980
981                         return XmlHelper.Decode (tableName);
982                 }
983
984                 private void ReserveSelfIdentity (XmlSchemaIdentityConstraint ic)
985                 {
986                         string tableName = GetSelectorTarget (ic.Selector.XPath);
987
988                         string [] cols = new string [ic.Fields.Count];
989                         bool [] isAttrSpec = new bool [cols.Length];
990
991                         int i = 0;
992                         foreach (XmlSchemaXPath Field in ic.Fields) {
993                                 string colName = Field.XPath;
994                                 bool isAttr = colName.Length > 0 && colName [0] == '@';
995                                 int index = colName.LastIndexOf (':');
996                                 if (index > 0)
997                                         colName = colName.Substring (index + 1);
998                                 else if (isAttr)
999                                         colName = colName.Substring (1);
1000
1001                                 colName = XmlHelper.Decode (colName);
1002                                 cols [i] = colName;
1003                                 isAttrSpec [i] = isAttr;
1004                                 i++;
1005                         }
1006                         
1007                         bool isPK = false;
1008                         // find if there is an attribute with the constraint name
1009                         // if not use the XmlSchemaConstraint's name.
1010                         string constraintName = ic.Name;
1011                         if (ic.UnhandledAttributes != null) {
1012                                 foreach (XmlAttribute attr in ic.UnhandledAttributes) {
1013                                         if (attr.NamespaceURI != XmlConstants.MsdataNamespace)
1014                                                 continue;
1015                                         switch (attr.LocalName) {
1016                                         case XmlConstants.ConstraintName:
1017                                                 constraintName = attr.Value;
1018                                                 break;
1019                                         case XmlConstants.PrimaryKey:
1020                                                 isPK = bool.Parse(attr.Value);
1021                                                 break;
1022                                         }
1023                                 }
1024                         }
1025                         reservedConstraints.Add (ic,
1026                                 new ConstraintStructure (tableName, cols,
1027                                         isAttrSpec, constraintName, isPK, null, false, false));
1028                 }
1029
1030                 private void ProcessSelfIdentity (ConstraintStructure c)
1031                 {
1032                         // Basic concept came from XmlSchemaMapper.cs
1033
1034                         string tableName = c.TableName;
1035                         
1036                         DataTable dt = dataset.Tables [tableName];
1037                         if (dt == null)
1038                                 throw new DataException (String.Format ("Invalid XPath selection inside selector. Cannot find: {0}", tableName));
1039
1040                         DataColumn [] cols = new DataColumn [c.Columns.Length];
1041                         for (int i = 0; i < cols.Length; i++) {
1042                                 string colName = c.Columns [i];
1043                                 bool isAttr = c.IsAttribute [i];
1044                                 DataColumn col = dt.Columns [colName];
1045                                 if (col == null)
1046                                         throw new DataException (String.Format ("Invalid XPath selection inside field. Cannot find: {0}", tableName));
1047                                 if (isAttr && col.ColumnMapping != MappingType.Attribute)
1048                                         throw new DataException ("The XPath specified attribute field, but mapping type is not attribute.");
1049                                 if (!isAttr && col.ColumnMapping != MappingType.Element)
1050                                         throw new DataException ("The XPath specified simple element field, but mapping type is not simple element.");
1051
1052                                 cols [i] = dt.Columns [colName];
1053                         }
1054                         
1055                         bool isPK = c.IsPrimaryKey;
1056                         string constraintName = c.ConstraintName;
1057                         dt.Constraints.Add (new UniqueConstraint (
1058                                 constraintName, cols, isPK));
1059                 }
1060
1061                 private void ReserveRelationIdentity (XmlSchemaElement element, XmlSchemaKeyref keyref)
1062                 {
1063                         // Basic concept came from XmlSchemaMapper.cs
1064
1065                         string tableName = GetSelectorTarget (keyref.Selector.XPath);
1066
1067                         string [] cols = new string [keyref.Fields.Count];
1068                         bool [] isAttrSpec = new bool [cols.Length];
1069                         int i = 0;
1070                         foreach (XmlSchemaXPath Field in keyref.Fields) {
1071                                 string colName = Field.XPath;
1072                                 bool isAttr = colName.Length > 0 && colName [0] == '@';
1073                                 int index = colName.LastIndexOf (':');
1074                                 if (index > 0)
1075                                         colName = colName.Substring (index + 1);
1076                                 else if (isAttr)
1077                                         colName = colName.Substring (1);
1078
1079                                 colName = XmlHelper.Decode (colName);
1080                                 cols [i] = colName;
1081                                 isAttrSpec [i] = isAttr;
1082                                 i++;
1083                         }
1084                         string constraintName = keyref.Name;
1085                         bool isNested = false;
1086                         bool isConstraintOnly = false;
1087                         if (keyref.UnhandledAttributes != null) {
1088                                 foreach (XmlAttribute attr in keyref.UnhandledAttributes) {
1089                                         if (attr.NamespaceURI != XmlConstants.MsdataNamespace)
1090                                                 continue;
1091                                         switch (attr.LocalName) {
1092                                         case XmlConstants.ConstraintName:
1093                                                 constraintName = attr.Value;
1094                                                 break;
1095                                         case XmlConstants.IsNested:
1096                                                 if (attr.Value == "true")
1097                                                         isNested = true;
1098                                                 break;
1099                                         case XmlConstants.ConstraintOnly:
1100                                                 if (attr.Value == "true")
1101                                                         isConstraintOnly = true;
1102                                                 break;
1103                                         }
1104                                 }
1105                         }
1106
1107                         reservedConstraints.Add (keyref, new ConstraintStructure (
1108                                 tableName, cols, isAttrSpec, constraintName,
1109                                 false, keyref.Refer.Name, isNested, isConstraintOnly));
1110                 }
1111
1112                 private void ProcessRelationIdentity (XmlSchemaElement element, ConstraintStructure c)
1113                 {
1114                         // Basic concept came from XmlSchemaMapper.cs
1115
1116                         string tableName = c.TableName;
1117
1118                         DataColumn [] cols;
1119                         DataTable dt = dataset.Tables [tableName];
1120                         if (dt == null)
1121                                 throw new DataException (String.Format ("Invalid XPath selection inside selector. Cannot find: {0}", tableName));
1122
1123                         cols = new DataColumn [c.Columns.Length];
1124                         for (int i = 0; i < cols.Length; i++) {
1125                                 string colName = c.Columns [i];
1126                                 bool isAttr = c.IsAttribute [i];
1127                                 DataColumn col = dt.Columns [colName];
1128                                 if (isAttr && col.ColumnMapping != MappingType.Attribute)
1129                                         throw new DataException ("The XPath specified attribute field, but mapping type is not attribute.");
1130                                 if (!isAttr && col.ColumnMapping != MappingType.Element)
1131                                         throw new DataException ("The XPath specified simple element field, but mapping type is not simple element.");
1132                                 cols [i] = col;
1133                         }
1134                         string name = c.ReferName;
1135                         // get the unique constraint for the releation
1136                         UniqueConstraint uniq = FindConstraint (name, element);
1137                         // generate the FK.
1138                         ForeignKeyConstraint fkc = new ForeignKeyConstraint(c.ConstraintName, uniq.Columns, cols);
1139                         dt.Constraints.Add (fkc);
1140
1141                         if (!c.IsConstraintOnly) {
1142                                 // generate the relation.
1143                                 DataRelation rel = new DataRelation (c.ConstraintName, uniq.Columns, cols, false);
1144                                 rel.Nested = c.IsNested;
1145                                 rel.SetParentKeyConstraint (uniq);
1146                                 rel.SetChildKeyConstraint (fkc);
1147
1148                                 dataset.Relations.Add (rel);
1149                         }
1150                 }
1151
1152                 // get the unique constraint for the relation.
1153                 // name - the name of the XmlSchemaUnique element
1154                 private UniqueConstraint FindConstraint (string name, XmlSchemaElement element)
1155                 {
1156                         // Copied from XmlSchemaMapper.cs
1157
1158                         // find the element in the constraint collection.
1159                         foreach (XmlSchemaIdentityConstraint c in element.Constraints) {
1160                                 if (c is XmlSchemaKeyref)
1161                                         continue;
1162
1163                                 if (c.Name == name) {
1164                                         string tableName = GetSelectorTarget (c.Selector.XPath);
1165
1166                                         // find the table in the dataset.
1167                                         DataTable dt = dataset.Tables [tableName];
1168
1169                                         string constraintName = c.Name;
1170                                         // find if there is an attribute with the constraint name
1171                                         // if not use the XmlSchemaUnique name.
1172                                         if (c.UnhandledAttributes != null)
1173                                                 foreach (XmlAttribute attr in c.UnhandledAttributes)
1174                                                         if (attr.LocalName == "ConstraintName" && attr.NamespaceURI == XmlConstants.MsdataNamespace)
1175                                                                 constraintName = attr.Value;
1176                                         return (UniqueConstraint) dt.Constraints [constraintName];
1177                                 }
1178                         }
1179                         throw new DataException ("Target identity constraint was not found: " + name);
1180                 }
1181
1182                 private void HandleAnnotations (XmlSchemaAnnotation an, bool nested)
1183                 {
1184                         foreach (XmlSchemaObject content in an.Items) {
1185                                 XmlSchemaAppInfo ai = content as XmlSchemaAppInfo;
1186                                 if (ai != null) {
1187                                         foreach (XmlNode n in ai.Markup) {
1188                                                 XmlElement el = n as XmlElement;
1189                                                 if (el != null && el.LocalName == "Relationship" && el.NamespaceURI == XmlConstants.MsdataNamespace)
1190                                                         HandleRelationshipAnnotation (el, nested);
1191                                         }
1192                                 }
1193                         }
1194                 }
1195
1196                 private void HandleRelationshipAnnotation (XmlElement el, bool nested)
1197                 {
1198                         string name = el.GetAttribute ("name");
1199                         string ptn = el.GetAttribute ("parent", XmlConstants.MsdataNamespace);
1200                         string ctn = el.GetAttribute ("child", XmlConstants.MsdataNamespace);
1201                         string pkn = el.GetAttribute ("parentkey", XmlConstants.MsdataNamespace);
1202                         string fkn = el.GetAttribute ("childkey", XmlConstants.MsdataNamespace);
1203
1204                         RelationStructure rel = new RelationStructure ();
1205                         rel.ExplicitName = XmlHelper.Decode (name);
1206                         rel.ParentTableName = XmlHelper.Decode (ptn);
1207                         rel.ChildTableName = XmlHelper.Decode (ctn);
1208                         // ColumnNames will be decoded wherever they are used as they can
1209                         // contain 'space' separated list of column-names.
1210                         rel.ParentColumnName = pkn;
1211                         rel.ChildColumnName = fkn;
1212                         rel.IsNested = nested;
1213                         rel.CreateConstraint = false; // by default?
1214                         relations.Add (rel);
1215                 }
1216
1217                 private object GetElementDefaultValue (XmlSchemaElement elem)
1218                 {
1219                         // Unlike attribute, element cannot have a default value.
1220                         if (elem.RefName == XmlQualifiedName.Empty)
1221                                 return elem.DefaultValue;
1222                         XmlSchemaElement referenced = schema.Elements [elem.RefName] as XmlSchemaElement;
1223                         if (referenced == null) // considering missing sub components
1224                                 return null;
1225                         return referenced.DefaultValue;
1226                 }
1227
1228                 private object GetAttributeDefaultValue (XmlSchemaAttribute attr)
1229                 {
1230 #if BUGGY_MS_COMPATIBLE
1231                         if (attr == null)
1232                                 return null;
1233                         else if (attr.RefName != XmlQualifiedName.Empty) {
1234                                 XmlSchemaAttribute referenced = schema.Attributes [attr.RefName] as XmlSchemaAttribute;
1235                                 if (referenced != null)
1236                                         return referenced.DefaultValue;
1237                                 else
1238                                         return null;
1239                         }
1240                         if (attr.DefaultValue != null)
1241                                 return attr.DefaultValue;
1242                         return attr.FixedValue;
1243 #else
1244                         if (attr.DefaultValue != null)
1245                                 return attr.DefaultValue;
1246                         else if (attr.FixedValue != null)
1247                                 return attr.FixedValue;
1248                         else if (attr.RefName == XmlQualifiedName.Empty)
1249                                 return null;
1250                         XmlSchemaAttribute referenced = schema.Attributes [attr.RefName] as XmlSchemaAttribute;
1251                         if (referenced == null) // considering missing sub components
1252                                 return null;
1253                         if (referenced.DefaultValue != null)
1254                                 return referenced.DefaultValue;
1255                         return referenced.FixedValue;
1256 #endif
1257                 }
1258         }
1259 }