[bcl] Remove more NET_2_0 checks from class libs
[mono.git] / mcs / class / System.Data / Test / System.Data / DataTableTest4.cs
1 // Authors:
2 //   Nagappan A <anagappan@novell.com>
3 //
4 // Copyright (c) 2007 Novell, Inc
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining
7 // a copy of this software and associated documentation files (the
8 // "Software"), to deal in the Software without restriction, including
9 // without limitation the rights to use, copy, modify, merge, publish,
10 // distribute, sublicense, and/or sell copies of the Software, and to
11 // permit persons to whom the Software is furnished to do so, subject to
12 // the following conditions:
13 //
14 // The above copyright notice and this permission notice shall be
15 // included in all copies or substantial portions of the Software.
16 //
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 //
25
26 using System;
27 using System.Data;
28 using System.Collections;
29 using System.IO;
30 using System.Xml;
31 using NUnit.Framework;
32
33 namespace MonoTests.System.Data
34 {
35         [TestFixture]
36         public class DataTableTest4
37         {
38                 string tempFile;
39                 DataSet dataSet;
40                 DataTable dummyTable;
41                 DataTable parentTable1;
42                 DataTable childTable;
43                 DataTable secondChildTable;
44
45                 [SetUp]
46                 public void SetUp ()
47                 {
48                         tempFile = Path.GetTempFileName ();
49                 }
50
51                 [TearDown]
52                 public void TearDown ()
53                 {
54                         if (tempFile != null)
55                                 File.Delete (tempFile);
56                 }
57         
58                 private void MakeParentTable1 ()
59                 {
60                         // Create a new Table
61                         parentTable1 = new DataTable ("ParentTable");
62                         dataSet = new DataSet ("XmlDataSet");
63                         DataColumn column;
64                         DataRow row;
65  
66                         // Create new DataColumn, set DataType, 
67                         // ColumnName and add to Table.
68                         column = new DataColumn ();
69                         column.DataType = typeof (int);
70                         column.ColumnName = "id";
71                         column.Unique = true;
72                         // Add the Column to the DataColumnCollection.
73                         parentTable1.Columns.Add (column);
74
75                         // Create second column
76                         column = new DataColumn ();
77                         column.DataType = typeof (string);
78                         column.ColumnName = "ParentItem";
79                         column.AutoIncrement = false;
80                         column.Caption = "ParentItem";
81                         column.Unique = false;
82                         // Add the column to the table
83                         parentTable1.Columns.Add (column);
84
85                         // Create third column.
86                         column = new DataColumn ();
87                         column.DataType = typeof (int);
88                         column.ColumnName = "DepartmentID";
89                         column.Caption = "DepartmentID";
90                         // Add the column to the table.
91                         parentTable1.Columns.Add (column);
92
93                         // Make the ID column the primary key column.
94                         DataColumn [] PrimaryKeyColumns = new DataColumn [2];
95                         PrimaryKeyColumns [0] = parentTable1.Columns ["id"];
96                         PrimaryKeyColumns [1] = parentTable1.Columns ["DepartmentID"];
97                         parentTable1.PrimaryKey = PrimaryKeyColumns;
98                         dataSet.Tables.Add (parentTable1);
99  
100                         // Create three new DataRow objects and add
101                         // them to the DataTable
102                         for (int i = 0; i <= 2; i++) {
103                                 row = parentTable1.NewRow ();
104                                 row ["id"] = i + 1 ;
105                                 row ["ParentItem"] = "ParentItem " + (i + 1);
106                                 row ["DepartmentID"] = i + 1;
107                                 parentTable1.Rows.Add (row);
108                         }
109                 }
110
111                 private void MakeDummyTable ()
112                 {
113                         // Create a new Table
114                         dataSet = new DataSet ();
115                         dummyTable = new DataTable ("DummyTable");
116                         DataColumn column;
117                         DataRow row;
118  
119                         // Create new DataColumn, set DataType, 
120                         // ColumnName and add to Table.
121                         column = new DataColumn ();
122                         column.DataType = typeof (int);
123                         column.ColumnName = "id";
124                         column.Unique = true;
125                         // Add the Column to the DataColumnCollection.
126                         dummyTable.Columns.Add (column);
127
128                         // Create second column
129                         column = new DataColumn ();
130                         column.DataType = typeof (string);
131                         column.ColumnName = "DummyItem";
132                         column.AutoIncrement = false;
133                         column.Caption = "DummyItem";
134                         column.Unique = false;
135                         // Add the column to the table
136                         dummyTable.Columns.Add (column);
137                         dataSet.Tables.Add (dummyTable);
138
139                         // Create three new DataRow objects and add 
140                         // them to the DataTable
141                         for (int i=0; i<=2; i++) {
142                                 row = dummyTable.NewRow ();
143                                 row ["id"] = i + 1 ;
144                                 row ["DummyItem"] = "DummyItem " + (i + 1);
145                                 dummyTable.Rows.Add (row);
146                         }
147
148                         DataRow row1 = dummyTable.Rows [1];
149                         dummyTable.AcceptChanges ();
150                         row1.BeginEdit ();
151                         row1 [1] = "Changed_DummyItem " + 2;
152                         row1.EndEdit ();
153                 }
154                 
155                 private void MakeChildTable ()
156                 {
157                         // Create a new Table
158                         childTable = new DataTable ("ChildTable");
159                         DataColumn column;
160                         DataRow row;
161  
162                         // Create first column and add to the DataTable.
163                         column = new DataColumn ();
164                         column.DataType= typeof (int);
165                         column.ColumnName = "ChildID";
166                         column.AutoIncrement = true;
167                         column.Caption = "ID";
168                         column.Unique = true;
169                         
170                         // Add the column to the DataColumnCollection
171                         childTable.Columns.Add (column);
172
173                         // Create second column
174                         column = new DataColumn ();
175                         column.DataType= typeof (string);
176                         column.ColumnName = "ChildItem";
177                         column.AutoIncrement = false;
178                         column.Caption = "ChildItem";
179                         column.Unique = false;
180                         childTable.Columns.Add (column);
181
182                         //Create third column
183                         column = new DataColumn ();
184                         column.DataType= typeof (int);
185                         column.ColumnName = "ParentID";
186                         column.AutoIncrement = false;
187                         column.Caption = "ParentID";
188                         column.Unique = false;
189                         childTable.Columns.Add (column);
190                         dataSet.Tables.Add (childTable);
191  
192                         // Create three sets of DataRow objects,
193                         // five rows each, and add to DataTable.
194                         for (int i = 0; i <= 1; i ++) {
195                                 row = childTable.NewRow ();
196                                 row ["childID"] = i + 1;
197                                 row ["ChildItem"] = "ChildItem " + (i + 1);
198                                 row ["ParentID"] = 1 ;
199                                 childTable.Rows.Add (row);
200                         }
201
202                         for (int i=0; i<=1; i ++) {
203                                 row = childTable.NewRow ();
204                                 row ["childID"] = i + 5;
205                                 row ["ChildItem"] = "ChildItem " + (i + 1);
206                                 row ["ParentID"] = 2 ;
207                                 childTable.Rows.Add (row);
208                         }
209
210                         for (int i = 0; i <= 1; i ++) {
211                                 row = childTable.NewRow ();
212                                 row ["childID"] = i + 10;
213                                 row ["ChildItem"] = "ChildItem " + (i + 1);
214                                 row ["ParentID"] = 3 ;
215                                 childTable.Rows.Add (row);
216                         }
217                 }
218
219                 private void MakeSecondChildTable ()
220                 {
221                         // Create a new Table
222                         secondChildTable = new DataTable ("SecondChildTable");
223                         DataColumn column;
224                         DataRow row;
225  
226                         // Create first column and add to the DataTable.
227                         column = new DataColumn ();
228                         column.DataType= typeof (int);
229                         column.ColumnName = "ChildID";
230                         column.AutoIncrement = true;
231                         column.Caption = "ID";
232                         column.ReadOnly = true;
233                         column.Unique = true;
234
235                         // Add the column to the DataColumnCollection.
236                         secondChildTable.Columns.Add (column);
237
238                         // Create second column.
239                         column = new DataColumn ();
240                         column.DataType= typeof (string);
241                         column.ColumnName = "ChildItem";
242                         column.AutoIncrement = false;
243                         column.Caption = "ChildItem";
244                         column.ReadOnly = false;
245                         column.Unique = false;
246                         secondChildTable.Columns.Add (column);
247
248                         //Create third column.
249                         column = new DataColumn ();
250                         column.DataType= typeof (int);
251                         column.ColumnName = "ParentID";
252                         column.AutoIncrement = false;
253                         column.Caption = "ParentID";
254                         column.ReadOnly = false;
255                         column.Unique = false;
256                         secondChildTable.Columns.Add (column);
257
258                         //Create fourth column.
259                         column = new DataColumn ();
260                         column.DataType= typeof (int);
261                         column.ColumnName = "DepartmentID";
262                         column.Caption = "DepartmentID";
263                         column.Unique = false;
264                         secondChildTable.Columns.Add (column);
265                         dataSet.Tables.Add (secondChildTable);
266
267                         // Create three sets of DataRow objects,
268                         // five rows each, and add to DataTable.
269                         for (int i = 0; i <= 1; i ++) {
270                                 row = secondChildTable.NewRow ();
271                                 row ["childID"] = i + 1;
272                                 row ["ChildItem"] = "SecondChildItem " + (i + 1);
273                                 row ["ParentID"] = 1 ;
274                                 row ["DepartmentID"] = 1;
275                                 secondChildTable.Rows.Add (row);
276                         }
277
278                         for (int i = 0; i <= 1; i++) {
279                                 row = secondChildTable.NewRow ();
280                                 row ["childID"] = i + 5;
281                                 row ["ChildItem"] = "SecondChildItem " + (i + 1);
282                                 row ["ParentID"] = 2;
283                                 row ["DepartmentID"] = 2;
284                                 secondChildTable.Rows.Add (row);
285                         }
286
287                         for (int i = 0; i <= 1; i++) {
288                                 row = secondChildTable.NewRow ();
289                                 row ["childID"] = i + 10;
290                                 row ["ChildItem"] = "SecondChildItem " + (i + 1);
291                                 row ["ParentID"] = 3 ;
292                                 row ["DepartmentID"] = 3;
293                                 secondChildTable.Rows.Add (row);
294                         }
295                 }
296
297                 private void MakeDataRelation ()
298                 {
299                         DataColumn parentColumn = dataSet.Tables ["ParentTable"].Columns ["id"];
300                         DataColumn childColumn = dataSet.Tables ["ChildTable"].Columns ["ParentID"];
301                         DataRelation relation = new DataRelation ("ParentChild_Relation1", parentColumn, childColumn);
302                         dataSet.Tables ["ChildTable"].ParentRelations.Add (relation);
303
304                         DataColumn [] parentColumn1 = new DataColumn [2];
305                         DataColumn [] childColumn1 = new DataColumn [2];
306
307                         parentColumn1 [0] = dataSet.Tables ["ParentTable"].Columns ["id"];
308                         parentColumn1 [1] = dataSet.Tables ["ParentTable"].Columns ["DepartmentID"];
309
310                         childColumn1 [0] = dataSet.Tables ["SecondChildTable"].Columns ["ParentID"];
311                         childColumn1 [1] = dataSet.Tables ["SecondChildTable"].Columns ["DepartmentID"];
312
313                         DataRelation secondRelation = new DataRelation("ParentChild_Relation2", parentColumn1, childColumn1);
314                         dataSet.Tables ["SecondChildTable"].ParentRelations.Add (secondRelation);
315                 }
316
317                 //Test properties of a table which does not belongs to a DataSet
318                 private void VerifyTableSchema (DataTable table, string tableName, DataSet ds)
319                 {
320                         //Test Schema 
321                         //Check Properties of Table
322                         Assert.AreEqual (string.Empty, table.Namespace, "#1");
323                         Assert.AreEqual (ds, table.DataSet, "#2");
324                         Assert.AreEqual (3, table.Columns.Count, "#3");
325                         Assert.AreEqual (false, table.CaseSensitive, "#5");
326                         Assert.AreEqual (tableName, table.TableName, "#6");
327                         Assert.AreEqual (2, table.Constraints.Count, "#7");
328                         Assert.AreEqual (string.Empty, table.Prefix, "#8");
329                         Assert.AreEqual ("Constraint1", table.Constraints [0].ToString (), "#9");
330                         Assert.AreEqual ("Constraint2", table.Constraints [1].ToString (), "#10");
331                         Assert.AreEqual (typeof (UniqueConstraint), table.Constraints [0].GetType (), "#11");
332                         Assert.AreEqual (typeof (UniqueConstraint), table.Constraints [1].GetType (), "#12");
333                         Assert.AreEqual (2, table.PrimaryKey.Length, "#13");
334                         Assert.AreEqual ("id", table.PrimaryKey [0].ToString (), "#14");
335                         Assert.AreEqual ("DepartmentID", table.PrimaryKey [1].ToString (), "#15");
336                         Assert.AreEqual (0, table.ParentRelations.Count, "#16");
337                         Assert.AreEqual (0, table.ChildRelations.Count, "#17");
338
339                         //Check properties of each column
340                         //First Column
341                         DataColumn col = table.Columns [0];
342                         Assert.AreEqual (false, col.AllowDBNull, "#18");
343                         Assert.AreEqual (false, col.AutoIncrement, "#19");
344                         Assert.AreEqual (0, col.AutoIncrementSeed, "#20");
345                         Assert.AreEqual (1, col.AutoIncrementStep, "#21");
346                         Assert.AreEqual ("Element", col.ColumnMapping.ToString (), "#22");
347                         Assert.AreEqual ("id", col.Caption, "#23");
348                         Assert.AreEqual ("id", col.ColumnName, "#24");
349                         Assert.AreEqual ("System.Int32", col.DataType.ToString (), "#25");
350                         Assert.AreEqual (string.Empty, col.DefaultValue.ToString (), "#26");
351                         Assert.AreEqual (false, col.DesignMode, "#27");
352                         Assert.AreEqual ("System.Data.PropertyCollection", col.ExtendedProperties.ToString (), "#28");
353                         Assert.AreEqual (-1, col.MaxLength, "#29");
354                         Assert.AreEqual (0, col.Ordinal, "#30");
355                         Assert.AreEqual (string.Empty, col.Prefix, "#31");
356                         Assert.AreEqual ("ParentTable", col.Table.ToString (), "#32");
357                         Assert.AreEqual (true, col.Unique, "#33");
358
359                         //Second Column
360                         col = table.Columns [1];
361                         Assert.AreEqual (true, col.AllowDBNull, "#34");
362                         Assert.AreEqual (false, col.AutoIncrement, "#35");
363                         Assert.AreEqual (0, col.AutoIncrementSeed, "#36");
364                         Assert.AreEqual (1, col.AutoIncrementStep, "#37");
365                         Assert.AreEqual ("Element", col.ColumnMapping.ToString (), "#38");
366                         Assert.AreEqual ("ParentItem", col.Caption, "#39");
367                         Assert.AreEqual ("ParentItem", col.ColumnName, "#40");
368                         Assert.AreEqual ("System.String", col.DataType.ToString (), "#41");
369                         Assert.AreEqual (string.Empty, col.DefaultValue.ToString (), "#42");
370                         Assert.AreEqual (false, col.DesignMode, "#43");
371                         Assert.AreEqual ("System.Data.PropertyCollection", col.ExtendedProperties.ToString (), "#44");
372                         Assert.AreEqual (-1, col.MaxLength, "#45");
373                         Assert.AreEqual (1, col.Ordinal, "#46");
374                         Assert.AreEqual (string.Empty, col.Prefix, "#47");
375                         Assert.AreEqual ("ParentTable", col.Table.ToString (), "#48");
376                         Assert.AreEqual (false, col.Unique, "#49");
377
378                         //Third Column
379                         col = table.Columns [2];
380                         Assert.AreEqual (false, col.AllowDBNull, "#50");
381                         Assert.AreEqual (false, col.AutoIncrement, "#51");
382                         Assert.AreEqual (0, col.AutoIncrementSeed, "#52");
383                         Assert.AreEqual (1, col.AutoIncrementStep, "#53");
384                         Assert.AreEqual ("Element", col.ColumnMapping.ToString (), "#54");
385                         Assert.AreEqual ("DepartmentID", col.Caption, "#55");
386                         Assert.AreEqual ("DepartmentID", col.ColumnName, "#56");
387                         Assert.AreEqual ("System.Int32", col.DataType.ToString (), "#57");
388                         Assert.AreEqual (string.Empty, col.DefaultValue.ToString (), "#58");
389                         Assert.AreEqual (false, col.DesignMode, "#59");
390                         Assert.AreEqual ("System.Data.PropertyCollection", col.ExtendedProperties.ToString (), "#60");
391                         Assert.AreEqual (-1, col.MaxLength, "#61");
392                         Assert.AreEqual (2, col.Ordinal, "#62");
393                         Assert.AreEqual (string.Empty, col.Prefix, "#63");
394                         Assert.AreEqual ("ParentTable", col.Table.ToString (), "#64");
395                         Assert.AreEqual (false, col.Unique, "#65");
396
397                         //Test the Xml
398                         Assert.AreEqual (3, table.Rows.Count, "#66");
399                         //Test values of each row
400                         DataRow row = table.Rows [0];
401                         Assert.AreEqual (1, row ["id"], "#67");
402                         Assert.AreEqual ("ParentItem 1", row ["ParentItem"], "#68");
403                         Assert.AreEqual (1, row ["DepartmentID"], "#69");
404
405                         row = table.Rows [1];
406                         Assert.AreEqual (2, row ["id"], "#70");
407                         Assert.AreEqual ("ParentItem 2", row ["ParentItem"], "#71");
408                         Assert.AreEqual (2, row ["DepartmentID"], "#72");
409
410                         row = table.Rows [2];
411                         Assert.AreEqual (3, row ["id"], "#73");
412                         Assert.AreEqual ("ParentItem 3", row ["ParentItem"], "#74");
413                         Assert.AreEqual (3, row ["DepartmentID"], "#75");
414                 }
415
416                 private void VerifyTable_WithChildren (DataTable table, string tableName, DataSet ds)
417                 {
418                         //Test Schema 
419                         //Check Properties of Table
420                         Assert.AreEqual (string.Empty, table.Namespace, "#1");
421                         Assert.AreEqual (ds.DataSetName, table.DataSet.DataSetName, "#2");
422                         Assert.AreEqual (3, table.Columns.Count, "#3");
423                         Assert.AreEqual (false, table.CaseSensitive, "#5");
424                         Assert.AreEqual (tableName, table.TableName, "#6");
425                         Assert.AreEqual (2, table.Constraints.Count, "#7");
426                         Assert.AreEqual (string.Empty, table.Prefix, "#8");
427                         Assert.AreEqual ("Constraint1", table.Constraints [0].ToString (), "#9");
428                         Assert.AreEqual ("Constraint2", table.Constraints [1].ToString (), "#10");
429                         Assert.AreEqual ("System.Data.UniqueConstraint", table.Constraints [0].GetType ().ToString (), "#11");
430                         Assert.AreEqual ("System.Data.UniqueConstraint", table.Constraints [1].GetType ().ToString (), "#12");
431                         Assert.AreEqual (2, table.PrimaryKey.Length, "#13");
432                         Assert.AreEqual ("id", table.PrimaryKey [0].ToString (), "#14");
433                         Assert.AreEqual ("DepartmentID", table.PrimaryKey [1].ToString (), "#15");
434                         Assert.AreEqual (0, table.ParentRelations.Count, "#16");
435                         Assert.AreEqual (2, table.ChildRelations.Count, "#17");
436
437                         //Check properties of each column
438                         //First Column
439                         DataColumn col = table.Columns [0];
440                         Assert.AreEqual (false, col.AllowDBNull, "#18");
441                         Assert.AreEqual (false, col.AutoIncrement, "#19");
442                         Assert.AreEqual (0, col.AutoIncrementSeed, "#20");
443                         Assert.AreEqual (1, col.AutoIncrementStep, "#21");
444                         Assert.AreEqual ("Element", col.ColumnMapping.ToString (), "#22");
445                         Assert.AreEqual ("id", col.Caption, "#23");
446                         Assert.AreEqual ("id", col.ColumnName, "#24");
447                         Assert.AreEqual (typeof (int), col.DataType, "#25");
448                         Assert.AreEqual (string.Empty, col.DefaultValue.ToString (), "#26");
449                         Assert.AreEqual (false, col.DesignMode, "#27");
450                         Assert.AreEqual ("System.Data.PropertyCollection", col.ExtendedProperties.ToString (), "#28");
451                         Assert.AreEqual (-1, col.MaxLength, "#29");
452                         Assert.AreEqual (0, col.Ordinal, "#30");
453                         Assert.AreEqual (string.Empty, col.Prefix, "#31");
454                         Assert.AreEqual ("ParentTable", col.Table.ToString (), "#32");
455                         Assert.AreEqual (true, col.Unique, "#33");
456
457                         //Second Column
458                         col = table.Columns [1];
459                         Assert.AreEqual (true, col.AllowDBNull, "#34");
460                         Assert.AreEqual (false, col.AutoIncrement, "#35");
461                         Assert.AreEqual (0, col.AutoIncrementSeed, "#36");
462                         Assert.AreEqual (1, col.AutoIncrementStep, "#37");
463                         Assert.AreEqual ("Element", col.ColumnMapping.ToString (), "#38");
464                         Assert.AreEqual ("ParentItem", col.Caption, "#39");
465                         Assert.AreEqual ("ParentItem", col.ColumnName, "#40");
466                         Assert.AreEqual (typeof (string), col.DataType, "#41");
467                         Assert.AreEqual (string.Empty, col.DefaultValue.ToString (), "#42");
468                         Assert.AreEqual (false, col.DesignMode, "#43");
469                         Assert.AreEqual ("System.Data.PropertyCollection", col.ExtendedProperties.ToString (), "#44");
470                         Assert.AreEqual (-1, col.MaxLength, "#45");
471                         Assert.AreEqual (1, col.Ordinal, "#46");
472                         Assert.AreEqual (string.Empty, col.Prefix, "#47");
473                         Assert.AreEqual ("ParentTable", col.Table.ToString (), "#48");
474                         Assert.AreEqual (false, col.Unique, "#49");
475
476                         //Third Column
477                         col = table.Columns [2];
478                         Assert.AreEqual (false, col.AllowDBNull, "#50");
479                         Assert.AreEqual (false, col.AutoIncrement, "#51");
480                         Assert.AreEqual (0, col.AutoIncrementSeed, "#52");
481                         Assert.AreEqual (1, col.AutoIncrementStep, "#53");
482                         Assert.AreEqual ("Element", col.ColumnMapping.ToString (), "#54");
483                         Assert.AreEqual ("DepartmentID", col.Caption, "#55");
484                         Assert.AreEqual ("DepartmentID", col.ColumnName, "#56");
485                         Assert.AreEqual (typeof (int), col.DataType, "#57");
486                         Assert.AreEqual (string.Empty, col.DefaultValue.ToString (), "#58");
487                         Assert.AreEqual (false, col.DesignMode, "#59");
488                         Assert.AreEqual ("System.Data.PropertyCollection", col.ExtendedProperties.ToString (), "#60");
489                         Assert.AreEqual (-1, col.MaxLength, "#61");
490                         Assert.AreEqual (2, col.Ordinal, "#62");
491                         Assert.AreEqual (string.Empty, col.Prefix, "#63");
492                         Assert.AreEqual ("ParentTable", col.Table.ToString (), "#64");
493                         Assert.AreEqual (false, col.Unique, "#65");
494
495                         //Test the Xml
496                         Assert.AreEqual (3, table.Rows.Count, "#66");
497                         //Test values of each row
498                         DataRow row = table.Rows [0];
499                         Assert.AreEqual (1, row ["id"], "#67");
500                         Assert.AreEqual ("ParentItem 1", row ["ParentItem"], "#68");
501                         Assert.AreEqual (1, row ["DepartmentID"], "#69");
502
503                         row = table.Rows [1];
504                         Assert.AreEqual (2, row ["id"], "#70");
505                         Assert.AreEqual ("ParentItem 2", row ["ParentItem"], "#71");
506                         Assert.AreEqual (2, row ["DepartmentID"], "#72");
507
508                         row = table.Rows [2];
509                         Assert.AreEqual (3, row ["id"], "#73");
510                         Assert.AreEqual ("ParentItem 3", row ["ParentItem"], "#74");
511                         Assert.AreEqual (3, row ["DepartmentID"], "#75");
512                 }
513
514                 private void VerifyDiffGramElement1 (XmlReader reader)
515                 {
516                         //This method checks the properties of the <id> element
517                         Assert.AreEqual (true, reader.IsStartElement (), "#1");
518                         Assert.AreEqual (3, reader.Depth, "#2");
519                         Assert.AreEqual (false, reader.HasAttributes, "#3");
520                         Assert.AreEqual (false, reader.HasValue, "#4");
521                         Assert.AreEqual (false, reader.IsDefault, "#5");
522                         Assert.AreEqual (false, reader.IsEmptyElement, "#6");
523                         Assert.AreEqual ("id", reader.Name, "#7");
524                         Assert.AreEqual ("id", reader.LocalName, "#8");
525                         Assert.AreEqual (XmlNodeType.Element, reader.NodeType, "#9");
526                 }
527
528                 private void VerifyDiffGramElement3 (XmlReader reader)
529                 {
530                         //This method checks the property of </id> end elem
531                         Assert.AreEqual (false, reader.IsStartElement (), "#1");
532                         Assert.AreEqual ("id", reader.Name, "#2");
533                         Assert.AreEqual ("id", reader.LocalName, "#3");
534                         Assert.AreEqual (XmlNodeType.EndElement, reader.NodeType, "#4");
535                 }
536
537                 private void VerifyDiffGramElement2 (XmlReader reader)
538                 {
539                         //This method tests the properties of the <DummyItem> elemnent
540                         Assert.AreEqual (true, reader.IsStartElement (), "#1");
541                         Assert.AreEqual (3, reader.Depth, "#2");
542                         Assert.AreEqual (false, reader.HasAttributes, "#3");
543                         Assert.AreEqual (false, reader.HasValue, "#4");
544                         Assert.AreEqual (false, reader.IsDefault, "#5");
545                         Assert.AreEqual (false, reader.IsEmptyElement, "#6");
546                         Assert.AreEqual ("DummyItem", reader.Name, "#7");
547                         Assert.AreEqual ("DummyItem", reader.LocalName, "#8");
548                         Assert.AreEqual (XmlNodeType.Element, reader.NodeType, "#9");
549                 }
550
551                 private void VerifyDiffGramElement4 (XmlReader reader)
552                 {
553                         //This method checks the properties of </DummyItem> end element
554                         Assert.AreEqual (false, reader.IsStartElement (), "#1");
555                         Assert.AreEqual ("DummyItem", reader.Name, "#2");
556                         Assert.AreEqual ("DummyItem", reader.LocalName, "#3");
557                         Assert.AreEqual (XmlNodeType.EndElement, reader.NodeType, "#4");
558                 }
559                 
560                 private void VerifyDiffGramElement5 (XmlReader reader)
561                 {
562                         //This method check the properties of </DummyTable> end element
563                         Assert.AreEqual (false, reader.IsStartElement (), "#1");
564                         Assert.AreEqual ("DummyTable", reader.Name, "#2");
565                         Assert.AreEqual ("DummyTable", reader.LocalName, "#3");
566                         Assert.AreEqual (XmlNodeType.EndElement, reader.NodeType, "#4");
567                 }
568
569                 [Test]
570                 public void XmlTest1 ()
571                 {
572                         MakeParentTable1 ();
573                         dataSet.Tables.Remove (parentTable1);
574
575                         using (FileStream stream = new FileStream (tempFile, FileMode.Create)) {
576                                 //Write the XML without any Schema information
577                                 parentTable1.WriteXml (stream);
578                         }
579
580                         DataTable table = new DataTable ();
581
582                         using (FileStream stream = new FileStream (tempFile, FileMode.Open)) {
583                                 try {
584                                         table.ReadXml (stream);
585                                         //Should throw an exception if the Xml
586                                         // File has no schema and target table
587                                         // too does not define any schema
588                                         Assert.Fail ("#1");
589                                 } catch (InvalidOperationException ex) {
590                                         Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
591                                         Assert.IsNull (ex.InnerException, "#3");
592                                         Assert.IsNotNull (ex.Message, "#4");
593                                 }
594                         }
595                 }
596
597                 [Test]
598                 public void XmlTest2 ()
599                 {
600                         //Make a table without any relations
601                         MakeParentTable1 ();
602                         dataSet.Tables.Remove (parentTable1);
603
604                         using (FileStream stream = new FileStream (tempFile, FileMode.Create)) {
605                                 //Write Xml along with the Schema
606                                 parentTable1.WriteXml (stream, XmlWriteMode.WriteSchema);
607                         }
608
609                         DataTable table = new DataTable ();
610                         //Read the Xml and the Schema into a table which does not belongs to any DataSet
611                         table.ReadXml (tempFile);
612                         VerifyTableSchema (table, parentTable1.TableName, parentTable1.DataSet);
613                 }
614
615                 [Test]
616                 public void XmlTest3 ()
617                 {
618                         //Make a table without any Relations
619                         MakeParentTable1 ();
620                         dataSet.Tables.Remove (parentTable1);
621
622                         using (FileStream stream = new FileStream (tempFile, FileMode.Create)) {
623                                 //Write the Xml and the Schema
624                                 parentTable1.WriteXml (stream, XmlWriteMode.WriteSchema);
625                         }
626
627                         DataTable table = new DataTable ();
628                         dataSet.Tables.Add (table);
629                         
630                         //Read the Xml and the Schema into a table which already belongs to a DataSet
631                         //and the table name does not match with the table ion the source XML 
632                         try {
633                                 table.ReadXml (tempFile);
634                                 Assert.Fail ("#1");
635                         } catch (ArgumentException ex) {
636                                 // DataTable 'Table1' does not match to any
637                                 // DataTable in source
638                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
639                                 Assert.IsNull (ex.InnerException, "#3");
640                                 Assert.IsNotNull (ex.Message, "#4");
641                                 Assert.IsTrue (ex.Message.IndexOf ("'Table1'") != -1, "#5");
642                                 Assert.IsNull (ex.ParamName, "#6");
643                         }
644                 }
645
646                 [Test]
647                 public void XmlTest4 ()
648                 {
649                         MakeParentTable1 ();
650
651                         using (FileStream stream = new FileStream (tempFile, FileMode.Create)) {
652                                 //Here the table belong to a dataset
653                                 //Write the Xml and the Schema
654                                 parentTable1.WriteXml (stream, XmlWriteMode.WriteSchema);
655                         }
656
657                         DataTable table = new DataTable ("ParentTable");
658                         //Read the Xml and the Schema into a table which already belongs to a DataSet
659                         //and the table name matches with the table in the source XML 
660                         table.ReadXml (tempFile);
661                         VerifyTableSchema (table, parentTable1.TableName, null);
662                 }
663
664                 [Test]
665                 public void XmlTest5 ()
666                 {
667                         //Create a parent table and create child tables
668                         MakeParentTable1 ();
669                         MakeChildTable ();
670                         MakeSecondChildTable ();
671                         //Relate the parent and the children
672                         MakeDataRelation ();
673
674                         using (FileStream stream = new FileStream (tempFile, FileMode.Create)) {
675                                 parentTable1.WriteXml (stream, XmlWriteMode.WriteSchema, false);
676                         }
677
678                         DataTable table = new DataTable ();
679                         table.ReadXml (tempFile);
680                         VerifyTableSchema (table, parentTable1.TableName, null);
681                 }
682
683                 [Test]
684                 public void XmlTest6 ()
685                 {
686                         //Create a parent table and create child tables
687                         MakeParentTable1 ();
688                         MakeChildTable ();
689                         MakeSecondChildTable ();
690                         //Relate the parent and the children
691                         MakeDataRelation ();
692
693                         using (FileStream stream = new FileStream (tempFile, FileMode.Create)) {
694                                 parentTable1.WriteXml (stream, XmlWriteMode.WriteSchema, true);
695                         }
696
697                         DataTable table = new DataTable ();
698                         table.ReadXml (tempFile);
699
700                         VerifyTable_WithChildren (table, parentTable1.TableName, parentTable1.DataSet);
701
702                         //Check Properties of First Child Table
703                         DataTable firstChildTable = table.ChildRelations [0].ChildTable;
704                         Assert.AreEqual (string.Empty, firstChildTable.Namespace, "#1");
705                         Assert.AreEqual ("XmlDataSet", firstChildTable.DataSet.DataSetName, "#2");
706                         Assert.AreEqual (3, firstChildTable.Columns.Count, "#3");
707                         Assert.AreEqual (typeof (int), firstChildTable.Columns [0].DataType, "#4");
708                         Assert.AreEqual (typeof (string), firstChildTable.Columns [1].DataType, "#5");
709                         Assert.AreEqual (typeof (int), firstChildTable.Columns [2].DataType, "#6");
710                         Assert.AreEqual (6, firstChildTable.Rows.Count, "#7");
711                         Assert.AreEqual (false, firstChildTable.CaseSensitive, "#8");
712                         Assert.AreEqual ("ChildTable", firstChildTable.TableName, "#9");
713                         Assert.AreEqual (string.Empty, firstChildTable.Prefix, "#10");
714                         Assert.AreEqual (2, firstChildTable.Constraints.Count, "#11");
715                         Assert.AreEqual ("Constraint1", firstChildTable.Constraints [0].ToString (), "#12");
716                         Assert.AreEqual ("ParentChild_Relation1", firstChildTable.Constraints [1].ToString (), "#13");
717                         Assert.AreEqual (1, firstChildTable.ParentRelations.Count, "#14");
718                         Assert.AreEqual ("ParentTable", firstChildTable.ParentRelations [0].ParentTable.TableName, "#15");
719                         Assert.AreEqual (0, firstChildTable.ChildRelations.Count, "#16");
720                         Assert.AreEqual (0, firstChildTable.PrimaryKey.Length, "#17");
721
722                         //Check Properties of Second Child Table
723                         DataTable secondChildTable = table.ChildRelations [1].ChildTable;
724                         Assert.AreEqual (string.Empty, secondChildTable.Namespace, "#18");
725                         Assert.AreEqual ("XmlDataSet", secondChildTable.DataSet.DataSetName, "#19");
726                         Assert.AreEqual (4, secondChildTable.Columns.Count, "#20");
727                         Assert.AreEqual (typeof (int), secondChildTable.Columns [0].DataType, "#21");
728                         Assert.AreEqual (typeof (string), secondChildTable.Columns [1].DataType, "#22");
729                         Assert.AreEqual (typeof (int), secondChildTable.Columns [2].DataType, "#23");
730                         Assert.AreEqual (typeof (int), secondChildTable.Columns [3].DataType, "#24");
731                         Assert.AreEqual (6, secondChildTable.Rows.Count, "#25");
732                         Assert.AreEqual (false, secondChildTable.CaseSensitive, "#26");
733                         Assert.AreEqual ("SecondChildTable", secondChildTable.TableName, "#27");
734                         Assert.AreEqual (string.Empty, secondChildTable.Prefix, "#28");
735                         Assert.AreEqual (2, secondChildTable.Constraints.Count, "#29");
736                         Assert.AreEqual ("Constraint1", secondChildTable.Constraints [0].ToString (), "#30");
737                         Assert.AreEqual ("ParentChild_Relation2", secondChildTable.Constraints [1].ToString (), "#31");
738                         Assert.AreEqual (1, secondChildTable.ParentRelations.Count, "#32");;
739                         Assert.AreEqual ("ParentTable", secondChildTable.ParentRelations [0].ParentTable.TableName, "#33");
740                         Assert.AreEqual (0, secondChildTable.ChildRelations.Count, "#34");
741                         Assert.AreEqual (0, secondChildTable.PrimaryKey.Length, "#35");
742                 }
743
744                 [Test]
745                 public void XmlTest7 ()
746                 {
747                         //Create a parent table and create child tables
748                         MakeParentTable1 ();
749                         MakeChildTable ();
750                         MakeSecondChildTable ();
751                         //Relate the parent and the children
752                         MakeDataRelation ();
753
754                         using (FileStream stream = new FileStream (tempFile, FileMode.Create)) {
755                                 //WriteXml on any of the children
756                                 childTable.WriteXml (stream, XmlWriteMode.WriteSchema, false);
757                         }
758
759                         DataTable table = new DataTable ();
760                         table.ReadXml (tempFile);
761
762                         //Test Schema 
763                         //Check Properties of Table
764                         Assert.AreEqual (string.Empty, table.Namespace, "#1");
765                         Assert.AreEqual (null, table.DataSet, "#2");
766                         Assert.AreEqual (3, table.Columns.Count, "#3");
767                         Assert.AreEqual (false, table.CaseSensitive, "#5");
768                         Assert.AreEqual ("ChildTable", table.TableName, "#6");
769                         Assert.AreEqual (string.Empty, table.Prefix, "#7");
770                         Assert.AreEqual (1, table.Constraints.Count, "#8");
771                         Assert.AreEqual ("Constraint1", table.Constraints [0].ToString (), "#9");
772                         Assert.AreEqual (typeof (UniqueConstraint), table.Constraints [0].GetType (), "#10");
773                         Assert.AreEqual (0, table.PrimaryKey.Length, "#11");
774                         Assert.AreEqual (0, table.ParentRelations.Count, "#12");
775                         Assert.AreEqual (0, table.ChildRelations.Count, "#13");
776
777                         
778                         //Check properties of each column
779                         //First Column
780                         DataColumn col = table.Columns [0];
781                         Assert.AreEqual (true, col.AllowDBNull, "#14");
782                         Assert.AreEqual (0, col.AutoIncrementSeed, "#15");
783                         Assert.AreEqual (1, col.AutoIncrementStep, "#16");
784                         Assert.AreEqual ("Element", col.ColumnMapping.ToString (), "#17");
785                         Assert.AreEqual ("ChildID", col.Caption, "#18");
786                         Assert.AreEqual ("ChildID", col.ColumnName, "#19");
787                         Assert.AreEqual (typeof (int), col.DataType, "#20");
788                         Assert.AreEqual (string.Empty, col.DefaultValue.ToString (), "#21");
789                         Assert.AreEqual (false, col.DesignMode, "#22");
790                         Assert.AreEqual ("System.Data.PropertyCollection", col.ExtendedProperties.ToString (), "#23");
791                         Assert.AreEqual (-1, col.MaxLength, "#24");
792                         Assert.AreEqual (0, col.Ordinal, "#25");
793                         Assert.AreEqual (string.Empty, col.Prefix, "#26");
794                         Assert.AreEqual ("ChildTable", col.Table.ToString (), "#27");
795                         Assert.AreEqual (true, col.Unique, "#28");
796
797                         //Second Column
798                         col = table.Columns [1];
799                         Assert.AreEqual (true, col.AllowDBNull, "#29");
800                         Assert.AreEqual (0, col.AutoIncrementSeed, "#30");
801                         Assert.AreEqual (1, col.AutoIncrementStep, "#31");
802                         Assert.AreEqual ("Element", col.ColumnMapping.ToString (), "#32");
803                         Assert.AreEqual ("ChildItem", col.Caption, "#33");
804                         Assert.AreEqual ("ChildItem", col.ColumnName, "#34");
805                         Assert.AreEqual (typeof (string), col.DataType, "#35");
806                         Assert.AreEqual (string.Empty, col.DefaultValue.ToString (), "#36");
807                         Assert.AreEqual (false, col.DesignMode, "#37");
808                         Assert.AreEqual ("System.Data.PropertyCollection", col.ExtendedProperties.ToString (), "#38");
809                         Assert.AreEqual (-1, col.MaxLength, "#39");
810                         Assert.AreEqual (1, col.Ordinal, "#40");
811                         Assert.AreEqual (string.Empty, col.Prefix, "#41");
812                         Assert.AreEqual ("ChildTable", col.Table.ToString (), "#42");
813                         Assert.AreEqual (false, col.Unique, "#43");
814
815                         //Third Column
816                         col = table.Columns [2];
817                         Assert.AreEqual (true, col.AllowDBNull, "#44");
818                         Assert.AreEqual (false, col.AutoIncrement, "#45");
819                         Assert.AreEqual (0, col.AutoIncrementSeed, "#46");
820                         Assert.AreEqual (1, col.AutoIncrementStep, "#47");
821                         Assert.AreEqual ("Element", col.ColumnMapping.ToString (), "#48");
822                         Assert.AreEqual ("ParentID", col.Caption, "#49");
823                         Assert.AreEqual ("ParentID", col.ColumnName, "#50");
824                         Assert.AreEqual (typeof (int), col.DataType, "#51");
825                         Assert.AreEqual (string.Empty, col.DefaultValue.ToString (), "#52");
826                         Assert.AreEqual (false, col.DesignMode, "#53");
827                         Assert.AreEqual ("System.Data.PropertyCollection", col.ExtendedProperties.ToString (), "#54");
828                         Assert.AreEqual (-1, col.MaxLength, "#55");
829                         Assert.AreEqual (2, col.Ordinal, "#56");
830                         Assert.AreEqual (string.Empty, col.Prefix, "#57");
831                         Assert.AreEqual ("ChildTable", col.Table.ToString (), "#58");
832                         Assert.AreEqual (false, col.Unique, "#59");
833
834                         //Test the Xml
835                         Assert.AreEqual (6, table.Rows.Count, "#60");
836
837                         //Test values of each row
838                         DataRow row = table.Rows [0];
839                         Assert.AreEqual (1, row ["ChildID"], "#61");
840                         Assert.AreEqual ("ChildItem 1", row ["ChildItem"], "#62");
841                         Assert.AreEqual (1, row ["ParentID"], "#63");
842
843                         row = table.Rows [1];
844                         Assert.AreEqual (2, row ["ChildID"], "#64");
845                         Assert.AreEqual ("ChildItem 2", row ["ChildItem"], "#65");
846                         Assert.AreEqual (1, row ["ParentID"], "#66");
847
848                         row = table.Rows [2];
849                         Assert.AreEqual (5, row ["ChildID"], "#67");
850                         Assert.AreEqual ("ChildItem 1", row ["ChildItem"], "#68");
851                         Assert.AreEqual (2, row ["ParentID"], "#69");
852
853                         row = table.Rows [3];
854                         Assert.AreEqual (6, row ["ChildID"], "#70");
855                         Assert.AreEqual ("ChildItem 2", row ["ChildItem"], "#71");
856                         Assert.AreEqual (2, row ["ParentID"], "#72");
857         
858                         row = table.Rows [4];
859                         Assert.AreEqual (10, row ["ChildID"], "#73");
860                         Assert.AreEqual ("ChildItem 1", row ["ChildItem"], "#74");
861                         Assert.AreEqual (3, row ["ParentID"], "#75");
862
863                         row = table.Rows [5];
864                         Assert.AreEqual (11, row ["ChildID"], "#75");
865                         Assert.AreEqual ("ChildItem 2", row ["ChildItem"], "#76");
866                         Assert.AreEqual (3, row ["ParentID"], "#77");
867                 }
868
869                 [Test]
870                 public void XmlTest8 ()
871                 {
872                         MakeParentTable1 ();
873                         MakeChildTable ();
874                         MakeSecondChildTable ();
875                         //Relate the parent and the children
876                         MakeDataRelation ();
877
878                         using (FileStream stream = new FileStream (tempFile, FileMode.Create)) {
879                                 //Write only the Xml
880                                 parentTable1.WriteXml (stream, XmlWriteMode.IgnoreSchema, false);
881                         }
882
883                         DataSet ds = new DataSet ();
884                         ds.ReadXml (tempFile);
885
886                         Assert.AreEqual (1, ds.Tables.Count, "#1");
887                         Assert.AreEqual ("ParentTable", ds.Tables [0].TableName, "#2");
888                         DataTable table = ds.Tables [0];
889
890                         Assert.AreEqual (3, table.Rows.Count, "#3");
891                         //Test values of each row
892                         DataRow row = table.Rows [0];
893                         Assert.AreEqual ("1", row ["id"], "#4");
894                         Assert.AreEqual ("ParentItem 1", row ["ParentItem"], "#5");
895                         Assert.AreEqual ("1", row ["DepartmentID"], "#6");
896
897                         row = table.Rows [1];
898                         Assert.AreEqual ("2", row ["id"], "#7");
899                         Assert.AreEqual ("ParentItem 2", row ["ParentItem"], "#8");
900                         Assert.AreEqual ("2", row ["DepartmentID"], "#9");
901
902                         row = table.Rows [2];
903                         Assert.AreEqual ("3", row ["id"], "#10");
904                         Assert.AreEqual ("ParentItem 3", row ["ParentItem"], "#11");
905                         Assert.AreEqual ("3", row ["DepartmentID"], "#12");
906                 }
907
908                 [Test]
909                 public void XmlTest9 ()
910                 {
911                         MakeParentTable1 ();
912                         MakeChildTable ();
913                         MakeSecondChildTable ();
914                         //Relate the parent and the children
915                         MakeDataRelation ();
916
917                         using (FileStream stream = new FileStream (tempFile, FileMode.Create)) {
918                                 //Write only the Xml
919                                 parentTable1.WriteXml (stream, XmlWriteMode.IgnoreSchema, true);
920                         }
921
922                         DataSet ds = new DataSet ();
923                         ds.ReadXml (tempFile);
924
925                         Assert.AreEqual (3, ds.Tables.Count, "#1");
926                         Assert.AreEqual ("ParentTable", ds.Tables [0].TableName, "#2");
927                         Assert.AreEqual ("ChildTable", ds.Tables [1].TableName, "#3");
928                         Assert.AreEqual ("SecondChildTable", ds.Tables [2].TableName, "#4");
929
930                         //get the first table
931                         DataTable table = ds.Tables [0];
932                         Assert.AreEqual (3, table.Rows.Count, "#5");
933                         
934                         DataRow row = table.Rows [0];
935                         Assert.AreEqual ("1", row ["id"], "#6");
936                         Assert.AreEqual ("ParentItem 1", row ["ParentItem"], "#7");
937                         Assert.AreEqual ("1", row ["DepartmentID"], "#8");
938
939                         row = table.Rows [1];
940                         Assert.AreEqual ("2", row ["id"], "#9");
941                         Assert.AreEqual ("ParentItem 2", row ["ParentItem"], "#10");
942                         Assert.AreEqual ("2", row ["DepartmentID"], "#11");
943
944                         row = table.Rows [2];
945                         Assert.AreEqual ("3", row ["id"], "#12");
946                         Assert.AreEqual ("ParentItem 3", row ["ParentItem"], "#13");
947                         Assert.AreEqual ("3", row ["DepartmentID"], "#14");
948
949                         //get the second table
950                         table = ds.Tables [1];
951                         Assert.AreEqual (6, table.Rows.Count);
952
953                         row = table.Rows [0];
954                         Assert.AreEqual ("1", row ["ChildID"], "#15");
955                         Assert.AreEqual ("ChildItem 1", row ["ChildItem"], "#16");
956                         Assert.AreEqual ("1", row ["ParentID"], "#17");
957
958                         row = table.Rows [1];
959                         Assert.AreEqual ("2", row ["ChildID"], "#18");
960                         Assert.AreEqual ("ChildItem 2", row ["ChildItem"], "#19");
961                         Assert.AreEqual ("1", row ["ParentID"], "#20");
962
963                         row = table.Rows [2];
964                         Assert.AreEqual ("5", row ["ChildID"], "#21");
965                         Assert.AreEqual ("ChildItem 1", row ["ChildItem"], "#22");
966                         Assert.AreEqual ("2", row ["ParentID"], "#23");
967
968                         row = table.Rows [3];
969                         Assert.AreEqual ("6", row ["ChildID"], "#24");
970                         Assert.AreEqual ("ChildItem 2", row ["ChildItem"], "#25");
971                         Assert.AreEqual ("2", row ["ParentID"], "#26");
972         
973                         row = table.Rows [4];
974                         Assert.AreEqual ("10", row ["ChildID"], "#27");
975                         Assert.AreEqual ("ChildItem 1", row ["ChildItem"], "#28");
976                         Assert.AreEqual ("3", row ["ParentID"], "#29");
977
978                         row = table.Rows [5];
979                         Assert.AreEqual ("11", row ["ChildID"], "#30");
980                         Assert.AreEqual ("ChildItem 2", row ["ChildItem"], "#31");
981                         Assert.AreEqual ("3", row ["ParentID"], "#32");
982
983                         //get the third table
984                         table = ds.Tables [2];
985                         Assert.AreEqual (6, table.Rows.Count);
986
987                         row = table.Rows [0];
988                         Assert.AreEqual ("1", row ["ChildID"], "#33");
989                         Assert.AreEqual ("SecondChildItem 1", row ["ChildItem"], "#34");
990                         Assert.AreEqual ("1", row ["ParentID"], "#35");
991                         Assert.AreEqual ("1", row ["DepartmentID"], "#36");
992
993                         row = table.Rows [1];
994                         Assert.AreEqual ("2", row ["ChildID"], "#37");
995                         Assert.AreEqual ("SecondChildItem 2", row ["ChildItem"], "#38");
996                         Assert.AreEqual ("1", row ["ParentID"], "#39");
997                         Assert.AreEqual ("1", row ["DepartmentID"], "#40");
998
999                         row = table.Rows [2];
1000                         Assert.AreEqual ("5", row ["ChildID"], "#41");
1001                         Assert.AreEqual ("SecondChildItem 1", row ["ChildItem"], "#42");
1002                         Assert.AreEqual ("2", row ["ParentID"], "#43");
1003                         Assert.AreEqual ("2", row ["DepartmentID"], "#44");
1004
1005                         row = table.Rows [3];
1006                         Assert.AreEqual ("6", row ["ChildID"], "#45");
1007                         Assert.AreEqual ("SecondChildItem 2", row ["ChildItem"], "#46");
1008                         Assert.AreEqual ("2", row ["ParentID"], "#47");
1009                         Assert.AreEqual ("2", row ["DepartmentID"], "#48");
1010         
1011                         row = table.Rows [4];
1012                         Assert.AreEqual ("10", row ["ChildID"], "#49");
1013                         Assert.AreEqual ("SecondChildItem 1", row ["ChildItem"], "#50");
1014                         Assert.AreEqual ("3", row ["ParentID"], "#51");
1015                         Assert.AreEqual ("3", row ["DepartmentID"], "#52");
1016
1017                         row = table.Rows [5];
1018                         Assert.AreEqual ("11", row ["ChildID"], "#53");
1019                         Assert.AreEqual ("SecondChildItem 2", row ["ChildItem"], "#54");
1020                         Assert.AreEqual ("3", row ["ParentID"], "#55");
1021                         Assert.AreEqual ("3", row ["DepartmentID"], "#56");
1022                 }
1023
1024                 [Test]
1025                 public void XmlTest10 ()
1026                 {
1027                         MakeDummyTable ();
1028
1029                         using (FileStream stream = new FileStream (tempFile, FileMode.Create)) {
1030                                 dummyTable.WriteXml (stream, XmlWriteMode.DiffGram);
1031                         }
1032
1033                         using (FileStream stream = new FileStream (tempFile, FileMode.Open)) {
1034                                 XmlReaderSettings settings = new XmlReaderSettings ();
1035                                 settings.IgnoreWhitespace = true;
1036                                 XmlReader reader = XmlReader.Create (stream, settings);
1037                         
1038                                 reader.Read ();
1039                                 Assert.AreEqual (true, reader.IsStartElement (), "#1");
1040                                 Assert.AreEqual (0, reader.Depth, "#2");
1041                                 Assert.AreEqual (true, reader.HasAttributes, "#3");
1042                                 Assert.AreEqual (false, reader.HasValue, "#4");
1043                                 Assert.AreEqual (false, reader.IsDefault, "#5");
1044                                 Assert.AreEqual (false, reader.IsEmptyElement, "#6");
1045                                 Assert.AreEqual ("diffgr:diffgram", reader.Name, "#7");
1046                                 Assert.AreEqual ("diffgram", reader.LocalName, "#8");
1047                                 Assert.AreEqual (2, reader.AttributeCount, "#9");
1048                                 Assert.AreEqual ("urn:schemas-microsoft-com:xml-msdata", reader [0], "#10");
1049                                 Assert.AreEqual ("urn:schemas-microsoft-com:xml-diffgram-v1", reader [1], "#11");
1050                                 Assert.AreEqual ("urn:schemas-microsoft-com:xml-diffgram-v1", reader.NamespaceURI, "#12");
1051                                 Assert.AreEqual (XmlNodeType.Element, reader.NodeType, "#13");
1052
1053                                 reader.Read ();
1054                                 Assert.AreEqual (true, reader.IsStartElement (), "#14");
1055                                 Assert.AreEqual (1, reader.Depth, "#15");
1056                                 Assert.AreEqual (false, reader.HasAttributes, "#16");
1057                                 Assert.AreEqual (false, reader.HasValue, "#17");
1058                                 Assert.AreEqual (false, reader.IsDefault, "#18");
1059                                 Assert.AreEqual (false, reader.IsEmptyElement, "#19");
1060                                 Assert.AreEqual ("NewDataSet", reader.Name, "#20");
1061                                 Assert.AreEqual ("NewDataSet", reader.LocalName, "#21");
1062                                 Assert.AreEqual (XmlNodeType.Element, reader.NodeType, "#22");
1063
1064                                 reader.Read ();
1065                                 Assert.AreEqual (true, reader.IsStartElement (), "#23");
1066                                 Assert.AreEqual (2, reader.Depth, "#24");
1067                                 Assert.AreEqual (true, reader.HasAttributes, "#25");
1068                                 Assert.AreEqual (false, reader.HasValue, "#26");
1069                                 Assert.AreEqual (false, reader.IsDefault, "#27");
1070                                 Assert.AreEqual (false, reader.IsEmptyElement, "#28");
1071                                 Assert.AreEqual ("DummyTable", reader.Name, "#29");
1072                                 Assert.AreEqual ("DummyTable", reader.LocalName, "#30");
1073                                 Assert.AreEqual (2, reader.AttributeCount, "#31");
1074                                 Assert.AreEqual ("DummyTable1", reader [0], "#32");
1075                                 Assert.AreEqual ("0", reader [1], "#33");
1076                                 Assert.AreEqual (XmlNodeType.Element, reader.NodeType, "#34");
1077
1078                                 reader.Read ();
1079                                 VerifyDiffGramElement1 (reader);
1080
1081                                 reader.Read ();
1082                                 Assert.AreEqual (XmlNodeType.Text, reader.NodeType, "#35");
1083                                 Assert.AreEqual (true, reader.HasValue, "#36");
1084                                 Assert.AreEqual ("1", reader.Value, "#37");
1085
1086                                 reader.Read ();
1087                                 VerifyDiffGramElement3 (reader);
1088                 
1089                                 reader.Read ();
1090                                 VerifyDiffGramElement2 (reader);
1091
1092                                 reader.Read ();
1093                                 Assert.AreEqual (XmlNodeType.Text, reader.NodeType, "#38");
1094                                 Assert.AreEqual (true, reader.HasValue, "#39");
1095                                 Assert.AreEqual ("DummyItem 1", reader.Value, "#40");
1096
1097                                 reader.Read ();
1098                                 Assert.AreEqual (false, reader.IsStartElement (), "#41");
1099                                 Assert.AreEqual ("DummyItem", reader.Name, "#42");
1100                                 Assert.AreEqual ("DummyItem", reader.LocalName, "#43");
1101                                 Assert.AreEqual (XmlNodeType.EndElement, reader.NodeType, "#44");
1102                 
1103                                 reader.Read ();
1104                                 Assert.AreEqual (false, reader.IsStartElement (), "#45");
1105                                 Assert.AreEqual ("DummyTable", reader.Name, "#46");
1106                                 Assert.AreEqual ("DummyTable", reader.LocalName, "#47");
1107                                 Assert.AreEqual (XmlNodeType.EndElement, reader.NodeType, "#48");
1108                         
1109                                 reader.Read ();
1110                                 Assert.AreEqual (true, reader.IsStartElement (), "#49");
1111                                 Assert.AreEqual (2, reader.Depth, "#50");
1112                                 Assert.AreEqual (true, reader.HasAttributes, "#51");
1113                                 Assert.AreEqual (false, reader.HasValue, "#52");
1114                                 Assert.AreEqual (false, reader.IsDefault, "#53");
1115                                 Assert.AreEqual (false, reader.IsEmptyElement, "#54");
1116                                 Assert.AreEqual ("DummyTable", reader.Name, "#55");
1117                                 Assert.AreEqual ("DummyTable", reader.LocalName, "#56");
1118                                 Assert.AreEqual (3, reader.AttributeCount, "#57");
1119                                 Assert.AreEqual ("DummyTable2", reader [0], "#58");
1120                                 Assert.AreEqual ("1", reader [1], "#59");
1121                                 Assert.AreEqual ("modified", reader [2], "#60");
1122                                 Assert.AreEqual (XmlNodeType.Element, reader.NodeType, "#61");
1123
1124                                 reader.Read ();
1125                                 VerifyDiffGramElement1 (reader);
1126                                  
1127                                 reader.Read ();
1128                                 Assert.AreEqual (XmlNodeType.Text, reader.NodeType, "#62");
1129                                 Assert.AreEqual (true, reader.HasValue, "#63");
1130                                 Assert.AreEqual ("2", reader.Value, "#64");
1131
1132                                 reader.Read ();
1133                                 VerifyDiffGramElement3 (reader);
1134                         
1135                                 reader.Read ();
1136                                 VerifyDiffGramElement2 (reader);
1137                 
1138                                 reader.Read ();
1139                                 Assert.AreEqual (XmlNodeType.Text, reader.NodeType, "#65");
1140                                 Assert.AreEqual (true, reader.HasValue, "#66");
1141                                 Assert.AreEqual ("Changed_DummyItem 2", reader.Value, "#67");
1142
1143                                 reader.Read ();
1144                                 VerifyDiffGramElement4 (reader);
1145
1146                                 reader.Read ();
1147                                 VerifyDiffGramElement5 (reader);
1148
1149                                 reader.Read ();
1150                                 Assert.AreEqual (true, reader.IsStartElement (), "#68");
1151                                 Assert.AreEqual (2, reader.Depth, "#69");
1152                                 Assert.AreEqual (true, reader.HasAttributes, "#70");
1153                                 Assert.AreEqual (false, reader.HasValue, "#71");
1154                                 Assert.AreEqual (false, reader.IsDefault, "#72");
1155                                 Assert.AreEqual (false, reader.IsEmptyElement, "#73");
1156                                 Assert.AreEqual ("DummyTable", reader.Name, "#74");
1157                                 Assert.AreEqual ("DummyTable", reader.LocalName, "#75");
1158                                 Assert.AreEqual (2, reader.AttributeCount, "#76");
1159                                 Assert.AreEqual ("DummyTable3", reader [0], "#77");
1160                                 Assert.AreEqual ("2", reader [1], "#78");
1161                                 Assert.AreEqual (XmlNodeType.Element, reader.NodeType, "#79");
1162
1163                                 reader.Read ();
1164                                 VerifyDiffGramElement1 (reader);
1165
1166                                 reader.Read ();
1167                                 Assert.AreEqual (XmlNodeType.Text, reader.NodeType, "#80");
1168                                 Assert.AreEqual (true, reader.HasValue, "#81");
1169                                 Assert.AreEqual ("3", reader.Value, "#82");
1170
1171                                 reader.Read ();
1172                                 VerifyDiffGramElement3 (reader);
1173
1174                                 reader.Read ();
1175                                 VerifyDiffGramElement2 (reader);
1176
1177                 
1178                                 reader.Read();
1179                                 Assert.AreEqual (XmlNodeType.Text, reader.NodeType, "#83");
1180                                 Assert.AreEqual (true, reader.HasValue, "#84");
1181                                 Assert.AreEqual ("DummyItem 3", reader.Value, "#85");
1182
1183                                 reader.Read ();
1184                                 VerifyDiffGramElement4 (reader);
1185
1186                                 reader.Read ();
1187                                 VerifyDiffGramElement5 (reader);
1188
1189                                 reader.Read ();
1190                                 Assert.AreEqual (false, reader.IsStartElement (), "#86");
1191                                 Assert.AreEqual ("NewDataSet", reader.Name, "#87");
1192                                 Assert.AreEqual ("NewDataSet", reader.LocalName, "#88");
1193                                 Assert.AreEqual (XmlNodeType.EndElement, reader.NodeType, "#89");
1194                 
1195                                 reader.Read ();
1196                                 Assert.AreEqual (true, reader.IsStartElement (), "#90");
1197                                 Assert.AreEqual (1, reader.Depth, "#91");
1198                                 Assert.AreEqual (false, reader.HasAttributes, "#92");
1199                                 Assert.AreEqual (false, reader.HasValue, "#93");
1200                                 Assert.AreEqual (false, reader.IsDefault, "#94");
1201                                 Assert.AreEqual (false, reader.IsEmptyElement, "#95");
1202                                 Assert.AreEqual ("diffgr:before", reader.Name, "#96");
1203                                 Assert.AreEqual ("before", reader.LocalName, "#97");
1204                                 Assert.AreEqual (XmlNodeType.Element, reader.NodeType, "#98");
1205
1206                                 reader.Read ();
1207                                 Assert.AreEqual (true, reader.IsStartElement (), "#99");
1208                                 Assert.AreEqual (2, reader.Depth, "#100");
1209                                 Assert.AreEqual (true, reader.HasAttributes, "#101");
1210                                 Assert.AreEqual (false, reader.HasValue, "#102");
1211                                 Assert.AreEqual (false, reader.IsDefault, "#103");
1212                                 Assert.AreEqual (false, reader.IsEmptyElement, "#104");
1213                                 Assert.AreEqual ("DummyTable", reader.Name, "#105");
1214                                 Assert.AreEqual ("DummyTable", reader.LocalName, "#106");
1215                                 Assert.AreEqual (2, reader.AttributeCount, "#107");
1216                                 Assert.AreEqual ("DummyTable2", reader [0], "#108");
1217                                 Assert.AreEqual ("1", reader [1], "#109");
1218                                 Assert.AreEqual (XmlNodeType.Element, reader.NodeType, "#110");
1219
1220                                 reader.Read ();
1221                                 VerifyDiffGramElement1 (reader);
1222
1223                                 reader.Read ();
1224                                 Assert.AreEqual (XmlNodeType.Text, reader.NodeType, "#111");
1225                                 Assert.AreEqual (true, reader.HasValue, "#112");
1226                                 Assert.AreEqual ("2", reader.Value, "#113");
1227
1228                                 reader.Read ();
1229                                 VerifyDiffGramElement3 (reader);
1230
1231                                 reader.Read ();
1232                                 VerifyDiffGramElement2 (reader);
1233
1234                                 reader.Read ();
1235                                 Assert.AreEqual (XmlNodeType.Text, reader.NodeType, "#114");
1236                                 Assert.AreEqual (true, reader.HasValue, "#115");
1237                                 Assert.AreEqual ("DummyItem 2", reader.Value, "#116");
1238                                 
1239                                 reader.Read ();
1240                                 VerifyDiffGramElement4 (reader);
1241                                 
1242                                 reader.Read ();
1243                                 VerifyDiffGramElement5 (reader);
1244
1245                                 reader.Read ();
1246                                 Assert.AreEqual (false, reader.IsStartElement (), "#117");
1247                                 Assert.AreEqual ("diffgr:before", reader.Name, "#118");
1248                                 Assert.AreEqual ("before", reader.LocalName, "#119");
1249                                 Assert.AreEqual (XmlNodeType.EndElement, reader.NodeType, "#120");
1250
1251                                 reader.Read ();
1252                                 Assert.AreEqual (false, reader.IsStartElement (), "#121");
1253                                 Assert.AreEqual ("diffgr:diffgram", reader.Name, "#122");
1254                                 Assert.AreEqual ("diffgram", reader.LocalName, "#123");
1255                                 Assert.AreEqual (XmlNodeType.EndElement, reader.NodeType, "#124");
1256                         }
1257                 }
1258
1259                 [Test]
1260                 public void XmlTest11 ()
1261                 {
1262                         MakeParentTable1 ();
1263
1264                         using (FileStream stream = new FileStream (tempFile, FileMode.Create)) {
1265                                 parentTable1.WriteXml (stream, XmlWriteMode.IgnoreSchema);
1266                         }
1267
1268                         XmlReadMode mode = XmlReadMode.Auto;
1269                         DataTable table = new DataTable ();
1270                         table.Columns.Add (new DataColumn ("id", typeof (int)));
1271
1272                         using (FileStream stream = new FileStream (tempFile, FileMode.Open)) {
1273                                 //This should not read anything as table name is not set        
1274                                 mode = table.ReadXml (stream);
1275                         }
1276
1277                         Assert.AreEqual (XmlReadMode.IgnoreSchema, mode, "#1");
1278                         Assert.AreEqual (String.Empty, table.TableName, "#2");
1279                         Assert.AreEqual (0, table.Rows.Count, "#3");
1280                         Assert.AreEqual (1, table.Columns.Count, "#4");
1281                         Assert.AreEqual (typeof (int), table.Columns [0].DataType, "#5");
1282                         Assert.IsNull (table.DataSet, "#6");
1283                 }
1284
1285                 [Test]
1286                 public void XmlTest12 ()
1287                 {
1288                         MakeParentTable1 ();
1289
1290                         using (FileStream stream = new FileStream (tempFile, FileMode.Create)) {
1291                                 parentTable1.WriteXml (stream, XmlWriteMode.IgnoreSchema);
1292                         }
1293
1294                         DataTable table = new DataTable ("Table1");
1295                         XmlReadMode mode = XmlReadMode.Auto;
1296                         table.Columns.Add (new DataColumn ("id", typeof (int)));
1297
1298                         using (FileStream stream = new FileStream (tempFile, FileMode.Open)) {
1299                                 //Same as last test. ReadXml does not read anything as table names dont match
1300                                 mode = table.ReadXml (stream);
1301                         }
1302
1303                         Assert.AreEqual (XmlReadMode.IgnoreSchema, mode, "#1");
1304                         Assert.AreEqual ("Table1", table.TableName, "#2");
1305                         Assert.AreEqual (0, table.Rows.Count, "#3");
1306                         Assert.AreEqual (1, table.Columns.Count, "#4");
1307                         Assert.AreEqual (typeof (int), table.Columns [0].DataType, "#5");
1308                         Assert.IsNull (table.DataSet, "#6");
1309                 }
1310
1311                 [Test]
1312                 public void XmlTest13 ()
1313                 {
1314                         MakeParentTable1 ();
1315
1316                         using (FileStream stream = new FileStream (tempFile, FileMode.Create)) {
1317                                 parentTable1.WriteXml (stream, XmlWriteMode.IgnoreSchema);
1318                         }
1319
1320                         DataTable table = new DataTable ("ParentTable");
1321                         XmlReadMode mode = XmlReadMode.Auto;
1322                         table.Columns.Add (new DataColumn ("id", typeof (string)));
1323
1324                         using (FileStream stream = new FileStream (tempFile, FileMode.Open)) {
1325                                 mode = table.ReadXml (stream);
1326                         }
1327
1328                         Assert.AreEqual (XmlReadMode.IgnoreSchema, mode, "#1");
1329                         Assert.AreEqual ("ParentTable", table.TableName, "#2");
1330                         Assert.AreEqual (3, table.Rows.Count, "#3");
1331                         Assert.AreEqual (1, table.Columns.Count, "#4");
1332                         Assert.AreEqual (typeof (string), table.Columns [0].DataType, "#5");
1333                         Assert.IsNull (table.DataSet, "#6");
1334
1335                         //Check Rows
1336                         DataRow row = table.Rows [0];
1337                         Assert.AreEqual ("1", row [0], "#7");
1338
1339                         row = table.Rows [1];
1340                         Assert.AreEqual ("2", row [0], "#8");
1341
1342                         row = table.Rows [2];
1343                         Assert.AreEqual ("3", row [0], "#9");
1344                 }
1345
1346                 [Test]
1347                 public void XmlTest14 ()
1348                 {
1349                         MakeParentTable1 ();
1350
1351                         using (FileStream stream = new FileStream (tempFile, FileMode.Create)) {
1352                                 parentTable1.WriteXml (stream, XmlWriteMode.IgnoreSchema);
1353                         }
1354
1355                         //Create a target table which has nomatching column(s) names
1356                         DataTable table = new DataTable ("ParentTable");
1357                         XmlReadMode mode = XmlReadMode.Auto;
1358                         table.Columns.Add (new DataColumn ("sid", typeof (string)));
1359
1360                         using (FileStream stream = new FileStream (tempFile, FileMode.Open)) {
1361                                 //ReadXml does not read anything as the column names are not matching
1362                                 mode = table.ReadXml (stream);
1363                         }
1364
1365                         Assert.AreEqual (XmlReadMode.IgnoreSchema, mode, "#1");
1366                         Assert.AreEqual ("ParentTable", table.TableName, "#2");
1367                         Assert.AreEqual (3, table.Rows.Count, "#3");
1368                         Assert.AreEqual (1, table.Columns.Count, "#4");
1369                         Assert.AreEqual ("sid", table.Columns [0].ColumnName, "#5");
1370                         Assert.AreEqual (typeof (string), table.Columns [0].DataType, "#6");
1371                         Assert.IsNull (table.DataSet, "#7");
1372
1373                         //Check the rows
1374                         foreach (DataRow row in table.Rows)
1375                                 Assert.IsNull (row [0], "#8");
1376                 }
1377
1378                 [Test]
1379                 public void XmlTest15 ()
1380                 {
1381                         MakeParentTable1 ();
1382
1383                         using (FileStream stream = new FileStream (tempFile, FileMode.Create)) {
1384                                 parentTable1.WriteXml (stream, XmlWriteMode.IgnoreSchema);
1385                         }
1386
1387                         //Create a target table which has matching column(s) name and an extra column
1388                         DataTable table = new DataTable ("ParentTable");
1389                         XmlReadMode mode = XmlReadMode.Auto;
1390                         table.Columns.Add (new DataColumn ("id", typeof (int)));
1391                         table.Columns.Add (new DataColumn ("ParentItem", typeof (string)));
1392                         table.Columns.Add (new DataColumn ("DepartmentID", typeof (int)));
1393                         table.Columns.Add (new DataColumn ("DummyColumn", typeof (string)));
1394
1395                         using (FileStream stream = new FileStream (tempFile, FileMode.Open)) {
1396                                 mode = table.ReadXml (stream);
1397                         }
1398
1399                         Assert.AreEqual (XmlReadMode.IgnoreSchema, mode, "#1");
1400                         Assert.AreEqual ("ParentTable", table.TableName, "#2");
1401                         Assert.AreEqual (3, table.Rows.Count, "#3");
1402                         Assert.AreEqual (4, table.Columns.Count, "#4");
1403                         Assert.IsNull (table.DataSet, "#5");
1404                 
1405                         //Check the Columns
1406                         Assert.AreEqual ("id", table.Columns [0].ColumnName, "#6");
1407                         Assert.AreEqual (typeof (int), table.Columns [0].DataType, "#7");
1408
1409                         Assert.AreEqual ("ParentItem", table.Columns [1].ColumnName, "#8");
1410                         Assert.AreEqual (typeof (string), table.Columns [1].DataType, "#9");
1411
1412                         Assert.AreEqual ("DepartmentID", table.Columns [2].ColumnName, "#10");
1413                         Assert.AreEqual (typeof (int), table.Columns [2].DataType, "#11");
1414
1415                         Assert.AreEqual ("DummyColumn", table.Columns [3].ColumnName, "#12");
1416                         Assert.AreEqual (typeof (string), table.Columns [3].DataType, "#13");
1417
1418                         //Check the rows
1419                         DataRow row = table.Rows [0];
1420                         Assert.AreEqual (1, row ["id"], "#14");
1421                         Assert.AreEqual ("ParentItem 1", row ["ParentItem"], "#15");
1422                         Assert.AreEqual (1, row ["DepartmentID"], "#16");
1423                         Assert.IsNull (row ["DummyColumn"], "#17");
1424
1425                         row = table.Rows [1];
1426                         Assert.AreEqual (2, row ["id"], "#18");
1427                         Assert.AreEqual ("ParentItem 2", row ["ParentItem"], "#19");
1428                         Assert.AreEqual (2, row ["DepartmentID"], "#20");
1429                         Assert.IsNull (row ["DummyColumn"], "#21");
1430
1431                         row = table.Rows [2];
1432                         Assert.AreEqual (3, row ["id"], "#22");
1433                         Assert.AreEqual ("ParentItem 3", row ["ParentItem"], "#23");
1434                         Assert.AreEqual (3, row ["DepartmentID"], "#24");
1435                         Assert.IsNull (row ["DummyColumn"], "#25");
1436                 }
1437
1438                 [Test]
1439                 public void XmlTest16 ()
1440                 {
1441                         MakeParentTable1 ();
1442
1443                         using (FileStream stream = new FileStream (tempFile, FileMode.Create)) {
1444                                 //Write the Xml with schema information
1445                                 parentTable1.WriteXml (stream, XmlWriteMode.WriteSchema);
1446                         }
1447
1448                         XmlReadMode mode = XmlReadMode.Auto ;
1449                         DataTable table = new DataTable ();
1450                         table.Columns.Add (new DataColumn ("id", typeof (int)));
1451
1452                         using (FileStream stream = new FileStream (tempFile, FileMode.Open)) {
1453                                 mode = table.ReadXml (stream);
1454                         }
1455
1456                         Assert.AreEqual (XmlReadMode.ReadSchema, mode, "#1");
1457                         Assert.AreEqual (String.Empty, table.TableName, "#2");
1458                         Assert.AreEqual (0, table.Rows.Count, "#3");
1459                         Assert.AreEqual (1, table.Columns.Count, "#4");
1460                         Assert.AreEqual (typeof (int), table.Columns [0].DataType, "#5");
1461                         Assert.IsNull (table.DataSet, "#6");
1462                 }
1463
1464                 [Test]
1465                 public void XmlTest17 ()
1466                 {
1467                         MakeParentTable1 ();
1468
1469                         using (FileStream stream = new FileStream (tempFile, FileMode.Create)) {
1470                                 parentTable1.WriteXml (stream, XmlWriteMode.WriteSchema);
1471                         }
1472
1473                         using (FileStream stream = new FileStream (tempFile, FileMode.Open)) {
1474                                 DataTable table = new DataTable ("Table1");
1475                                 table.Columns.Add (new DataColumn ("id", typeof (int)));
1476
1477                                 try {
1478                                         table.ReadXml (stream);
1479                                         Assert.Fail ("#1");
1480                                 } catch (ArgumentException ex) {
1481                                         // DataTable 'Table1' does not match to
1482                                         // any DataTable in source
1483                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1484                                         Assert.IsNull (ex.InnerException, "#3");
1485                                         Assert.IsNotNull (ex.Message, "#4");
1486                                         Assert.IsTrue (ex.Message.IndexOf ("'Table1'") != -1, "#5");
1487                                         Assert.IsNull (ex.ParamName, "#6");
1488                                 }
1489                         }
1490                 }
1491
1492                 [Test]
1493                 public void XmlTest18 ()
1494                 {
1495                         MakeParentTable1 ();
1496
1497                         using (FileStream stream = new FileStream (tempFile, FileMode.Create)) {
1498                                 parentTable1.WriteXml (stream, XmlWriteMode.WriteSchema);
1499                         }
1500
1501                         DataTable table = new DataTable ("ParentTable");
1502                         XmlReadMode mode = XmlReadMode.Auto;
1503                         table.Columns.Add (new DataColumn ("id", typeof (int)));
1504                         table.Columns.Add (new DataColumn ("DepartmentID", typeof (int)));
1505
1506                         using (FileStream stream = new FileStream (tempFile, FileMode.Open)) {
1507                                 mode = table.ReadXml (stream);
1508                         }
1509
1510                         Assert.AreEqual (XmlReadMode.ReadSchema, mode, "#1");
1511                         Assert.AreEqual ("ParentTable", table.TableName, "#2");
1512                         Assert.AreEqual (3, table.Rows.Count, "#3");
1513                         Assert.AreEqual (2, table.Columns.Count, "#4");
1514                         Assert.AreEqual (typeof (int), table.Columns [0].DataType, "#5");
1515                         Assert.AreEqual (typeof (int), table.Columns [1].DataType, "#6");
1516                         Assert.IsNull (table.DataSet, "#6");
1517
1518                         //Check rows
1519                         DataRow row = table.Rows [0];
1520                         Assert.AreEqual (1, row [0], "#7");
1521                         Assert.AreEqual (1, row [1], "#8");
1522                         
1523                         row = table.Rows [1];
1524                         Assert.AreEqual (2, row [0], "#9");
1525                         Assert.AreEqual (2, row [1], "#10");
1526
1527                         row = table.Rows [2];
1528                         Assert.AreEqual (3, row [0], "#11");
1529                         Assert.AreEqual (3, row [1], "#12");
1530                 }
1531                 
1532                 [Test]
1533                 public void XmlTest19 ()
1534                 {
1535                         MakeParentTable1 ();
1536
1537                         using (FileStream stream = new FileStream (tempFile, FileMode.Create)) {
1538                                 parentTable1.WriteXml (stream, XmlWriteMode.IgnoreSchema);
1539                         }
1540
1541                         DataSet ds = new DataSet ();
1542                         DataTable table = new DataTable ();
1543                         XmlReadMode mode = XmlReadMode.Auto;
1544                         table.Columns.Add (new DataColumn ("id", typeof (int)));
1545                         ds.Tables.Add (table);
1546
1547                         using (FileStream stream = new FileStream (tempFile, FileMode.Open)) {
1548                                 //ReadXml wont read anything as TableNames dont match
1549                                 mode = table.ReadXml (stream);
1550                         }
1551
1552                         Assert.AreEqual (XmlReadMode.IgnoreSchema, mode, "#1");
1553                         Assert.AreEqual ("Table1", table.TableName, "#2");
1554                         Assert.AreEqual (0, table.Rows.Count, "#3");
1555                         Assert.AreEqual (1, table.Columns.Count, "#4");
1556                         Assert.AreEqual (typeof (int), table.Columns [0].DataType, "#5");
1557                         Assert.AreEqual ("System.Data.DataSet", table.DataSet.ToString (), "#6");
1558                         Assert.AreEqual ("NewDataSet", table.DataSet.DataSetName, "#7");
1559                 }
1560
1561                 [Test]
1562                 public void XmlTest20 ()
1563                 {
1564                         MakeParentTable1 ();
1565
1566                         using (FileStream stream = new FileStream (tempFile, FileMode.Create)) {
1567                                 parentTable1.WriteXml (stream, XmlWriteMode.IgnoreSchema);
1568                         }
1569
1570                         DataSet ds = new DataSet ();
1571                         DataTable table = new DataTable ("HelloWorldTable");
1572                         XmlReadMode mode = XmlReadMode.Auto;
1573                         table.Columns.Add (new DataColumn ("id", typeof (int)));
1574                         ds.Tables.Add (table);
1575
1576                         using (FileStream stream = new FileStream (tempFile, FileMode.Open)) {
1577                                 //ReadXml wont read anything as TableNames dont match
1578                                 mode = table.ReadXml (stream);
1579                         }
1580
1581                         Assert.AreEqual (XmlReadMode.IgnoreSchema, mode, "#1");
1582                         Assert.AreEqual ("HelloWorldTable", table.TableName, "#2");
1583                         Assert.AreEqual (0, table.Rows.Count, "#3");
1584                         Assert.AreEqual (1, table.Columns.Count, "#4");
1585                         Assert.AreEqual (typeof (int), table.Columns [0].DataType, "#5");
1586                         Assert.AreEqual ("System.Data.DataSet", table.DataSet.ToString (), "#6");
1587                         Assert.AreEqual ("NewDataSet", table.DataSet.DataSetName, "#7");
1588                 }
1589
1590                 [Test]
1591                 public void XmlTest21 ()
1592                 {
1593                         MakeParentTable1 ();
1594
1595                         using (FileStream stream = new FileStream (tempFile, FileMode.Create)) {
1596                                 parentTable1.WriteXml (stream, XmlWriteMode.IgnoreSchema);
1597                         }
1598
1599                         DataSet ds = new DataSet ();
1600                         DataTable table = new DataTable ("ParentTable");
1601                         XmlReadMode mode = XmlReadMode.Auto;
1602                         table.Columns.Add (new DataColumn ("id", Type.GetType ("System.Int32")));
1603                         ds.Tables.Add (table);
1604
1605                         using (FileStream stream = new FileStream (tempFile, FileMode.Open)) {
1606                                 mode = table.ReadXml (stream);
1607                         }
1608
1609                         Assert.AreEqual (XmlReadMode.IgnoreSchema, mode, "#1");
1610                         Assert.AreEqual ("ParentTable", table.TableName, "#2");
1611                         Assert.AreEqual (3, table.Rows.Count, "#3");
1612                         Assert.AreEqual (1, table.Columns.Count, "#4");
1613                         Assert.AreEqual (typeof (int), table.Columns [0].DataType, "#5");
1614                         Assert.AreEqual ("System.Data.DataSet", table.DataSet.ToString (), "#6");
1615                         Assert.AreEqual ("NewDataSet", table.DataSet.DataSetName, "#7");
1616
1617                         //Check the rows
1618                         DataRow row = table.Rows [0];
1619                         Assert.AreEqual (1, row [0], "#8");
1620                         
1621                         row = table.Rows [1];
1622                         Assert.AreEqual (2, row [0], "#9");
1623
1624                         row = table.Rows [2];
1625                         Assert.AreEqual (3, row [0], "#10");
1626                 }
1627
1628                 [Test]
1629                 public void XmlTest22 ()
1630                 {
1631                         MakeParentTable1 ();
1632                         
1633                         using (FileStream stream = new FileStream (tempFile, FileMode.Create)) {
1634                                 parentTable1.WriteXml (stream, XmlWriteMode.WriteSchema);
1635                         }
1636
1637                         using (FileStream stream = new FileStream (tempFile, FileMode.Open)) {
1638                                 DataSet ds = new DataSet ();
1639                                 DataTable table = new DataTable ("Table1");
1640                                 table.Columns.Add (new DataColumn ("id", Type.GetType ("System.Int32")));
1641                                 ds.Tables.Add (table);
1642
1643                                 try {
1644                                         table.ReadXml (stream);
1645                                         Assert.Fail ("#1");
1646                                 } catch (ArgumentException ex) {
1647                                         // DataTable 'Table1' does not match to
1648                                         // any DataTable in source
1649                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1650                                         Assert.IsNull (ex.InnerException, "#3");
1651                                         Assert.IsNotNull (ex.Message, "#4");
1652                                         Assert.IsTrue (ex.Message.IndexOf ("'Table1'") != -1, "#5");
1653                                         Assert.IsNull (ex.ParamName, "#6");
1654                                 }
1655                         }
1656                 }
1657
1658                 [Test]
1659                 public void XmlTest23 ()
1660                 {
1661                         MakeParentTable1 ();
1662
1663                         using (FileStream stream = new FileStream (tempFile, FileMode.Create)) {
1664                                 parentTable1.WriteXml (stream, XmlWriteMode.WriteSchema);
1665                         }
1666
1667                         DataSet ds = new DataSet ();
1668                         DataTable table = new DataTable ("ParentTable");
1669                         XmlReadMode mode = XmlReadMode.Auto;
1670                         table.Columns.Add (new DataColumn ("id", typeof (int)));
1671                         table.Columns.Add (new DataColumn ("DepartmentID", typeof (string)));
1672                         ds.Tables.Add (table);
1673
1674                         using (FileStream stream = new FileStream (tempFile, FileMode.Open)) {
1675                                 mode = table.ReadXml (stream);
1676                         }
1677
1678                         Assert.AreEqual (XmlReadMode.ReadSchema, mode, "#1");
1679                         Assert.AreEqual ("ParentTable", table.TableName, "#2");
1680                         Assert.AreEqual ("NewDataSet", table.DataSet.DataSetName, "#3");
1681                         Assert.AreEqual (3, table.Rows.Count, "#4");
1682                         Assert.AreEqual (2, table.Columns.Count, "#5");
1683                         Assert.AreEqual (typeof (int), table.Columns [0].DataType, "#6");
1684                         Assert.AreEqual (typeof (string), table.Columns [1].DataType, "#7");
1685
1686                         //Check rows
1687                         DataRow row = table.Rows [0];
1688                         Assert.AreEqual (1, row [0], "#8");
1689                         Assert.AreEqual ("1", row [1], "#9");
1690                         
1691                         row = table.Rows [1];
1692                         Assert.AreEqual (2, row [0], "#10");
1693                         Assert.AreEqual ("2", row [1], "#11");
1694
1695                         row = table.Rows [2];
1696                         Assert.AreEqual (3, row [0], "#12");
1697                         Assert.AreEqual ("3", row [1], "#13");
1698                 }
1699
1700                 [Test]
1701                 public void XmlTest24 ()
1702                 {
1703                         MakeDummyTable ();
1704
1705                         using (FileStream stream = new FileStream (tempFile, FileMode.Create)) {
1706                                 dummyTable.WriteXml (stream, XmlWriteMode.DiffGram);
1707                         }
1708
1709                         //This test is the same for case when the table name is set but no schema is defined
1710
1711                         using (FileStream stream = new FileStream (tempFile, FileMode.Open)) {
1712                                 DataTable table = new DataTable ();
1713
1714                                 try {
1715                                         table.ReadXml (stream);
1716                                         Assert.Fail ("#1");
1717                                 } catch (InvalidOperationException ex) {
1718                                         Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
1719                                         Assert.IsNull (ex.InnerException, "#3");
1720                                         Assert.IsNotNull (ex.Message, "#4");
1721                                 }
1722                         }
1723                 }
1724
1725                 [Test]
1726                 public void XmlTest25 ()
1727                 {
1728                         MakeDummyTable ();
1729
1730                         using (FileStream stream = new FileStream (tempFile, FileMode.Create)) {
1731                                 dummyTable.WriteXml (stream, XmlWriteMode.DiffGram);
1732                         }
1733
1734                         XmlReadMode mode = XmlReadMode.Auto;
1735                         //Create a table but dont set the table name
1736                         DataTable table = new DataTable ();
1737                         //define the table schame partially
1738                         table.Columns.Add ("id", typeof (int));
1739                         
1740                         using (FileStream stream = new FileStream (tempFile, FileMode.Open)) {
1741                                 mode = table.ReadXml (stream);
1742                         }
1743                 
1744                         Assert.AreEqual (String.Empty, table.TableName, "#1");
1745                         Assert.AreEqual (1, table.Columns.Count, "#2");
1746                         Assert.AreEqual (0, table.Rows.Count, "#3");
1747                         Assert.AreEqual (XmlReadMode.DiffGram, mode, "#4");
1748                 }
1749
1750                 [Test]
1751                 public void XmlTest26 ()
1752                 {
1753                         MakeDummyTable ();
1754
1755                         using (FileStream stream = new FileStream (tempFile, FileMode.Create)) {
1756                                 dummyTable.WriteXml (stream, XmlWriteMode.DiffGram);
1757                         }
1758
1759                         //Create a table and set the table name
1760                         DataTable table = new DataTable ("DummyTable");
1761                         //define the table schame partially
1762                         table.Columns.Add (new DataColumn ("DummyItem", typeof (string)));
1763
1764                         XmlReadMode mode = XmlReadMode.Auto;
1765
1766                         using (FileStream stream = new FileStream (tempFile, FileMode.Open)) {
1767                                 mode = table.ReadXml (stream);
1768                         }
1769                 
1770                         Assert.AreEqual (XmlReadMode.DiffGram, mode, "#1");
1771                         Assert.AreEqual (null, table.DataSet, "#2");
1772                         Assert.AreEqual (1, table.Columns.Count, "#3");
1773                         Assert.AreEqual (typeof (string), table.Columns [0].DataType, "#4");
1774                         Assert.AreEqual (3, table.Rows.Count, "#5");
1775
1776                         //Check Rows
1777                         DataRow row = table.Rows [0];
1778                         Assert.AreEqual ("DummyItem 1", row [0], "#1");
1779                         Assert.AreEqual (DataRowState.Unchanged, row.RowState, "#2");
1780
1781                         row = table.Rows [1];
1782                         Assert.AreEqual ("Changed_DummyItem 2", row [0], "#3");
1783                         Assert.AreEqual (DataRowState.Modified, row.RowState, "#4");
1784
1785                         row = table.Rows [2];
1786                         Assert.AreEqual ("DummyItem 3", row [0], "#5");
1787                         Assert.AreEqual (DataRowState.Unchanged, row.RowState, "#6");
1788                 }
1789
1790                 [Test]
1791                 public void XmlTest27 ()
1792                 {
1793                         MakeDummyTable ();
1794
1795                         using (FileStream stream = new FileStream (tempFile, FileMode.Create)) {
1796                                 dummyTable.WriteXml (stream, XmlWriteMode.DiffGram);
1797                         }
1798
1799                         //Create a table and set the table name
1800                         DataTable table = new DataTable ("DummyTable");
1801                         //define the table and add an extra column in the table
1802                         table.Columns.Add (new DataColumn ("id", typeof (int)));
1803                         table.Columns.Add (new DataColumn ("DummyItem", typeof (string)));
1804                         //Add an extra column which does not match any column in the source diffram
1805                         table.Columns.Add (new DataColumn ("ExtraColumn", typeof (double)));
1806
1807                         XmlReadMode mode = XmlReadMode.Auto;
1808
1809                         using (FileStream stream = new FileStream (tempFile, FileMode.Open)) {
1810                                 mode = table.ReadXml (stream);
1811                         }
1812                 
1813                         Assert.AreEqual (XmlReadMode.DiffGram, mode, "#1");
1814                         Assert.IsNull (table.DataSet, "#2");
1815                         Assert.AreEqual (3, table.Columns.Count, "#3");
1816                         Assert.AreEqual (typeof (int), table.Columns [0].DataType, "#4");
1817                         Assert.AreEqual (typeof (string), table.Columns [1].DataType, "#5");
1818                         Assert.AreEqual (typeof (double), table.Columns [2].DataType, "#6");
1819                         Assert.AreEqual (3, table.Rows.Count, "#7");
1820
1821                         //Check Rows
1822                         DataRow row = table.Rows [0];
1823                         Assert.AreEqual (1, row [0], "#8");
1824                         Assert.AreEqual ("DummyItem 1", row [1], "#9");
1825                         Assert.AreSame (DBNull.Value, row [2], "#10");
1826                         Assert.AreEqual (DataRowState.Unchanged, row.RowState, "#11");
1827
1828                         row = table.Rows [1];
1829                         Assert.AreEqual (2, row [0], "#12");
1830                         Assert.AreEqual ("Changed_DummyItem 2", row [1], "#13");
1831                         Assert.AreSame (DBNull.Value, row [2], "#14");
1832                         Assert.AreEqual (DataRowState.Modified, row.RowState, "#15");
1833
1834                         row = table.Rows [2];
1835                         Assert.AreEqual (3, row [0], "#16");
1836                         Assert.AreEqual ("DummyItem 3", row [1], "#17");
1837                         Assert.AreSame (DBNull.Value, row [2], "#18");
1838                         Assert.AreEqual (DataRowState.Unchanged, row.RowState, "#19");
1839                 }
1840
1841                 [Test]
1842                 public void XmlTest28 ()
1843                 {
1844                         MakeDummyTable ();
1845
1846                         using (FileStream stream = new FileStream (tempFile, FileMode.Create)) {
1847                                 dummyTable.WriteXml (stream, XmlWriteMode.DiffGram);
1848                         }
1849
1850                         //Create a table and set the table name
1851                         DataTable table = new DataTable ("DummyTable");
1852                         //define the table schame partially with a column name which does not match with any
1853                         //table columns in the diffgram
1854                         table.Columns.Add (new DataColumn ("WrongColumnName", Type.GetType ("System.String")));
1855
1856                         XmlReadMode mode = XmlReadMode.Auto;
1857
1858                         using (FileStream stream = new FileStream (tempFile, FileMode.Open)) {
1859                                 mode = table.ReadXml (stream);
1860                         }
1861
1862                         Assert.AreEqual (XmlReadMode.DiffGram, mode, "#1");
1863                         Assert.IsNull (table.DataSet, "#2");
1864                         Assert.AreEqual ("DummyTable", table.TableName, "#3");
1865                         Assert.AreEqual (1, table.Columns.Count, "#4");
1866                         Assert.AreEqual (typeof (string), table.Columns [0].DataType, "#5");
1867                 
1868                         Assert.AreEqual (3, table.Rows.Count, "#6");
1869                         foreach (DataRow row in table.Rows)
1870                                 Assert.AreSame (DBNull.Value, row [0], "#7");
1871                 }
1872
1873                 [Test]
1874                 public void XmlTest29 ()
1875                 {
1876                         MakeParentTable1 ();
1877                         MakeChildTable ();
1878                         MakeSecondChildTable ();
1879                         MakeDataRelation ();
1880                         
1881                         using (FileStream stream = new FileStream (tempFile, FileMode.Create)) {
1882                                 parentTable1.WriteXml (stream, XmlWriteMode.DiffGram, true);
1883                         }
1884                         
1885                         DataTable table1 = new DataTable ("ParentTable");
1886                         table1.Columns.Add (new DataColumn (parentTable1.Columns [0].ColumnName, typeof (int)));
1887                         table1.Columns.Add (new DataColumn (parentTable1.Columns [1].ColumnName, typeof (string)));
1888                         table1.Columns.Add (new DataColumn (parentTable1.Columns [2].ColumnName, typeof (int)));
1889
1890                         //ReadXml on a DiffGram will never create any child relation
1891                         XmlReadMode mode = table1.ReadXml (tempFile);
1892
1893                         Assert.AreEqual (XmlReadMode.DiffGram, mode, "#1");
1894                         Assert.AreEqual (null, table1.DataSet, "#2");
1895                         Assert.AreEqual ("ParentTable", table1.TableName, "#3");
1896                         Assert.AreEqual (3, table1.Columns.Count, "#4");
1897                         Assert.AreEqual (typeof (int), table1.Columns [0].DataType, "#5");
1898                         Assert.AreEqual (typeof (string), table1.Columns [1].DataType, "#6");
1899                         Assert.AreEqual (typeof (int), table1.Columns [2].DataType, "#7");
1900                         Assert.AreEqual (0, table1.ChildRelations.Count, "#8");
1901
1902                         Assert.AreEqual (3, table1.Rows.Count, "#9");
1903                         //Check the row
1904                         DataRow row = table1.Rows [0];
1905                         Assert.AreEqual (1, row [0], "#10");
1906                         Assert.AreEqual ("ParentItem 1", row [1], "#11");
1907                         Assert.AreEqual (1, row [2], "#12");
1908
1909                         row = table1.Rows [1];
1910                         Assert.AreEqual (2, row [0], "#13");
1911                         Assert.AreEqual ("ParentItem 2", row [1], "#14");
1912                         Assert.AreEqual (2, row [2], "#15");
1913
1914                         row = table1.Rows [2];
1915                         Assert.AreEqual (3, row [0], "#16");
1916                         Assert.AreEqual ("ParentItem 3", row [1], "#17");
1917                         Assert.AreEqual (3, row [2], "#18");
1918                 }
1919
1920                 [Test]
1921                 public void XmlTest30 ()
1922                 {
1923                         MakeDummyTable ();
1924
1925                         using (FileStream stream = new FileStream (tempFile, FileMode.Create)) {
1926                                 dummyTable.WriteXml (stream, XmlWriteMode.DiffGram);
1927                         }
1928                         
1929                         Assert.AreEqual (3, dummyTable.Rows.Count, "#4b");
1930
1931                         DataSet dataSet = new DataSet ("HelloWorldDataSet");
1932                         DataTable table = new DataTable ("DummyTable");
1933                         table.Columns.Add (new DataColumn ("DummyItem", typeof (string)));
1934                         dataSet.Tables.Add (table);
1935
1936                         //Call ReadXml on a table which belong to a DataSet
1937                         table.ReadXml (tempFile);
1938
1939                         Assert.AreEqual ("HelloWorldDataSet", table.DataSet.DataSetName, "#1");
1940                         Assert.AreEqual (1, table.Columns.Count, "#2");
1941                         Assert.AreEqual (typeof (string), table.Columns [0].DataType, "#3");
1942                         Assert.AreEqual (3, table.Rows.Count, "#4");
1943
1944                         //Check Rows
1945                         DataRow row = table.Rows [0];
1946                         Assert.AreEqual ("DummyItem 1", row [0], "#5");
1947                         Assert.AreEqual (DataRowState.Unchanged, row.RowState, "#6");
1948
1949                         row = table.Rows [1];
1950                         Assert.AreEqual ("Changed_DummyItem 2", row [0], "#7");
1951                         Assert.AreEqual (DataRowState.Modified, row.RowState, "#8");
1952
1953                         row = table.Rows [2];
1954                         Assert.AreEqual ("DummyItem 3", row [0], "#9");
1955                         Assert.AreEqual (DataRowState.Unchanged, row.RowState, "#10");
1956                 }
1957
1958                 [Test]
1959                 public void XmlTest31 ()
1960                 {
1961                         DataSet ds = new DataSet ();
1962                         DataTable parent = new DataTable ("Parent");
1963                         parent.Columns.Add (new DataColumn ("col1", typeof (int)));
1964                         parent.Columns.Add (new DataColumn ("col2", typeof (string)));
1965                         parent.Columns [0].Unique = true;
1966
1967                         DataTable child1 = new DataTable ("Child1");
1968                         child1.Columns.Add (new DataColumn ("col3", typeof (int)));
1969                         child1.Columns.Add (new DataColumn ("col4", typeof (string)));
1970                         child1.Columns.Add (new DataColumn ("col5", typeof (int)));
1971                         child1.Columns [2].Unique = true;
1972
1973                         DataTable child2 = new DataTable ("Child2");
1974                         child2.Columns.Add (new DataColumn ("col6", typeof (int)));
1975                         child2.Columns.Add (new DataColumn ("col7"));
1976
1977                         parent.Rows.Add (new object [] {1, "P_"});
1978                         parent.Rows.Add (new object [] {2, "P_"});
1979
1980                         child1.Rows.Add (new object [] {1, "C1_", 3});
1981                         child1.Rows.Add (new object [] {1, "C1_", 4});
1982                         child1.Rows.Add (new object [] {2, "C1_", 5});
1983                         child1.Rows.Add (new object [] {2, "C1_", 6});
1984         
1985                         child2.Rows.Add (new object [] {3, "C2_"});
1986                         child2.Rows.Add (new object [] {3, "C2_"});
1987                         child2.Rows.Add (new object [] {4, "C2_"});
1988                         child2.Rows.Add (new object [] {4, "C2_"});
1989                         child2.Rows.Add (new object [] {5, "C2_"});
1990                         child2.Rows.Add (new object [] {5, "C2_"});
1991                         child2.Rows.Add (new object [] {6, "C2_"});
1992                         child2.Rows.Add (new object [] {6, "C2_"});
1993
1994                         ds.Tables.Add (parent);
1995                         ds.Tables.Add (child1);
1996                         ds.Tables.Add (child2);
1997
1998                         DataRelation relation = new DataRelation ("Relation1", parent.Columns [0], child1.Columns [0]);
1999                         parent.ChildRelations.Add (relation);
2000
2001                         relation = new DataRelation ("Relation2", child1.Columns [2], child2.Columns [0]);
2002                         child1.ChildRelations.Add (relation);
2003
2004                         using (FileStream stream = new FileStream (tempFile, FileMode.Create)) {
2005                                 parent.WriteXml (stream, XmlWriteMode.WriteSchema, true);
2006                         }
2007
2008                         DataTable table = new DataTable ();
2009                         table.ReadXml (tempFile);
2010
2011                         Assert.AreEqual ("Parent", table.TableName, "#1");
2012                         Assert.AreEqual ("NewDataSet", table.DataSet.DataSetName, "#2");
2013                         Assert.AreEqual (2, table.Columns.Count, "#3");
2014                         Assert.AreEqual (2, table.Rows.Count, "#4");
2015                         Assert.AreEqual (typeof (Int32), table.Columns [0].DataType, "#5");
2016                         Assert.AreEqual (typeof (String), table.Columns [1].DataType, "#6");
2017                         Assert.AreEqual (1, table.Constraints.Count, "#7");
2018                         Assert.AreEqual (typeof (UniqueConstraint), table.Constraints [0].GetType (), "#8");
2019                         Assert.AreEqual (1, table.ChildRelations.Count, "#9");
2020                         Assert.AreEqual ("Relation1", table.ChildRelations [0].RelationName, "#10");
2021                         Assert.AreEqual ("Parent", table.ChildRelations [0].ParentTable.TableName, "#11");
2022                         Assert.AreEqual ("Child1", table.ChildRelations [0].ChildTable.TableName, "#12");
2023                 
2024                         DataTable table1 = table.ChildRelations [0].ChildTable;
2025                         Assert.AreEqual ("Child1", table1.TableName, "#13");
2026                         Assert.AreEqual ("NewDataSet", table1.DataSet.DataSetName, "#14");
2027                         Assert.AreEqual (3, table1.Columns.Count, "#15");
2028                         Assert.AreEqual (4, table1.Rows.Count, "#16");
2029                         Assert.AreEqual (typeof (Int32), table1.Columns [0].DataType, "#17");
2030                         Assert.AreEqual (typeof (String), table1.Columns [1].DataType, "#18");
2031                         Assert.AreEqual (typeof (Int32), table1.Columns [2].DataType, "#19");
2032                         Assert.AreEqual (2, table1.Constraints.Count, "#20");
2033                         Assert.AreEqual (typeof (UniqueConstraint), table1.Constraints [0].GetType (), "#21");
2034                         Assert.AreEqual (typeof (ForeignKeyConstraint), table1.Constraints [1].GetType (), "#22");
2035                         Assert.AreEqual (1, table1.ParentRelations.Count, "#23");
2036                         Assert.AreEqual (1, table1.ChildRelations.Count, "#24");
2037                         Assert.AreEqual ("Relation1", table1.ParentRelations [0].RelationName, "#25");
2038                         Assert.AreEqual ("Relation2", table1.ChildRelations [0].RelationName, "#26");
2039                         Assert.AreEqual ("Parent", table1.ParentRelations [0].ParentTable.TableName, "#27");
2040                         Assert.AreEqual ("Child2", table1.ChildRelations [0].ChildTable.TableName, "#28");
2041         
2042                         table1 = table1.ChildRelations [0].ChildTable;
2043                         Assert.AreEqual ("Child2", table1.TableName, "#28");
2044                         Assert.AreEqual ("NewDataSet", table1.DataSet.DataSetName, "#29");
2045                         Assert.AreEqual (2, table1.Columns.Count, "#30");
2046                         Assert.AreEqual (8, table1.Rows.Count, "#31");
2047                         Assert.AreEqual (typeof (Int32), table1.Columns [0].DataType, "#32");
2048                         Assert.AreEqual (typeof (String), table1.Columns [1].DataType, "#33");
2049                         Assert.AreEqual (1, table1.Constraints.Count, "#34");
2050                         Assert.AreEqual (typeof (ForeignKeyConstraint), table1.Constraints [0].GetType (), "#35");
2051                         Assert.AreEqual (1, table1.ParentRelations.Count, "#36");
2052                         Assert.AreEqual (0, table1.ChildRelations.Count, "#37");
2053                         Assert.AreEqual ("Relation2", table1.ParentRelations [0].RelationName, "#38");
2054                         Assert.AreEqual ("Child1", table1.ParentRelations [0].ParentTable.TableName, "#39");
2055                         
2056                 }
2057         }
2058 }