本文整理汇总了C#中DataTable.ReadXml方法的典型用法代码示例。如果您正苦于以下问题:C# DataTable.ReadXml方法的具体用法?C# DataTable.ReadXml怎么用?C# DataTable.ReadXml使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DataTable
的用法示例。
在下文中一共展示了DataTable.ReadXml方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: XmlTest11
public void XmlTest11()
{
MakeParentTable1();
using (FileStream stream = new FileStream(_tempFile, FileMode.Create))
{
_parentTable1.WriteXml(stream, XmlWriteMode.IgnoreSchema);
}
XmlReadMode mode = XmlReadMode.Auto;
DataTable table = new DataTable();
table.Columns.Add(new DataColumn("id", typeof(int)));
using (FileStream stream = new FileStream(_tempFile, FileMode.Open))
{
//This should not read anything as table name is not set
mode = table.ReadXml(stream);
}
Assert.Equal(XmlReadMode.IgnoreSchema, mode);
Assert.Equal(string.Empty, table.TableName);
Assert.Equal(0, table.Rows.Count);
Assert.Equal(1, table.Columns.Count);
Assert.Equal(typeof(int), table.Columns[0].DataType);
Assert.Null(table.DataSet);
}
示例2: XmlTest27
public void XmlTest27()
{
MakeDummyTable();
using (FileStream stream = new FileStream(_tempFile, FileMode.Create))
{
_dummyTable.WriteXml(stream, XmlWriteMode.DiffGram);
}
//Create a table and set the table name
DataTable table = new DataTable("DummyTable");
//define the table and add an extra column in the table
table.Columns.Add(new DataColumn("id", typeof(int)));
table.Columns.Add(new DataColumn("DummyItem", typeof(string)));
//Add an extra column which does not match any column in the source diffram
table.Columns.Add(new DataColumn("ExtraColumn", typeof(double)));
XmlReadMode mode = XmlReadMode.Auto;
using (FileStream stream = new FileStream(_tempFile, FileMode.Open))
{
mode = table.ReadXml(stream);
}
Assert.Equal(XmlReadMode.DiffGram, mode);
Assert.Null(table.DataSet);
Assert.Equal(3, table.Columns.Count);
Assert.Equal(typeof(int), table.Columns[0].DataType);
Assert.Equal(typeof(string), table.Columns[1].DataType);
Assert.Equal(typeof(double), table.Columns[2].DataType);
Assert.Equal(3, table.Rows.Count);
//Check Rows
DataRow row = table.Rows[0];
Assert.Equal(1, row[0]);
Assert.Equal("DummyItem 1", row[1]);
Assert.Same(DBNull.Value, row[2]);
Assert.Equal(DataRowState.Unchanged, row.RowState);
row = table.Rows[1];
Assert.Equal(2, row[0]);
Assert.Equal("Changed_DummyItem 2", row[1]);
Assert.Same(DBNull.Value, row[2]);
Assert.Equal(DataRowState.Modified, row.RowState);
row = table.Rows[2];
Assert.Equal(3, row[0]);
Assert.Equal("DummyItem 3", row[1]);
Assert.Same(DBNull.Value, row[2]);
Assert.Equal(DataRowState.Unchanged, row.RowState);
}
示例3: XmlTest24
public void XmlTest24()
{
MakeDummyTable();
using (FileStream stream = new FileStream(_tempFile, FileMode.Create))
{
_dummyTable.WriteXml(stream, XmlWriteMode.DiffGram);
}
//This test is the same for case when the table name is set but no schema is defined
using (FileStream stream = new FileStream(_tempFile, FileMode.Open))
{
DataTable table = new DataTable();
try
{
table.ReadXml(stream);
Assert.False(true);
}
catch (InvalidOperationException ex)
{
Assert.Equal(typeof(InvalidOperationException), ex.GetType());
Assert.Null(ex.InnerException);
Assert.NotNull(ex.Message);
}
}
}
示例4: XmlTest25
public void XmlTest25()
{
MakeDummyTable();
using (FileStream stream = new FileStream(_tempFile, FileMode.Create))
{
_dummyTable.WriteXml(stream, XmlWriteMode.DiffGram);
}
XmlReadMode mode = XmlReadMode.Auto;
//Create a table but dont set the table name
DataTable table = new DataTable();
//define the table schame partially
table.Columns.Add("id", typeof(int));
using (FileStream stream = new FileStream(_tempFile, FileMode.Open))
{
mode = table.ReadXml(stream);
}
Assert.Equal(string.Empty, table.TableName);
Assert.Equal(1, table.Columns.Count);
Assert.Equal(0, table.Rows.Count);
Assert.Equal(XmlReadMode.DiffGram, mode);
}
示例5: XmlTest22
public void XmlTest22()
{
MakeParentTable1();
using (FileStream stream = new FileStream(_tempFile, FileMode.Create))
{
_parentTable1.WriteXml(stream, XmlWriteMode.WriteSchema);
}
using (FileStream stream = new FileStream(_tempFile, FileMode.Open))
{
DataSet ds = new DataSet();
DataTable table = new DataTable("Table1");
table.Columns.Add(new DataColumn("id", Type.GetType("System.Int32")));
ds.Tables.Add(table);
try
{
table.ReadXml(stream);
Assert.False(true);
}
catch (ArgumentException ex)
{
// DataTable 'Table1' does not match to
// any DataTable in source
Assert.Equal(typeof(ArgumentException), ex.GetType());
Assert.Null(ex.InnerException);
Assert.NotNull(ex.Message);
Assert.True(ex.Message.IndexOf("'Table1'") != -1);
Assert.Null(ex.ParamName);
}
}
}
示例6: XmlTest23
public void XmlTest23()
{
MakeParentTable1();
using (FileStream stream = new FileStream(_tempFile, FileMode.Create))
{
_parentTable1.WriteXml(stream, XmlWriteMode.WriteSchema);
}
DataSet ds = new DataSet();
DataTable table = new DataTable("ParentTable");
XmlReadMode mode = XmlReadMode.Auto;
table.Columns.Add(new DataColumn("id", typeof(int)));
table.Columns.Add(new DataColumn("DepartmentID", typeof(string)));
ds.Tables.Add(table);
using (FileStream stream = new FileStream(_tempFile, FileMode.Open))
{
mode = table.ReadXml(stream);
}
Assert.Equal(XmlReadMode.ReadSchema, mode);
Assert.Equal("ParentTable", table.TableName);
Assert.Equal("NewDataSet", table.DataSet.DataSetName);
Assert.Equal(3, table.Rows.Count);
Assert.Equal(2, table.Columns.Count);
Assert.Equal(typeof(int), table.Columns[0].DataType);
Assert.Equal(typeof(string), table.Columns[1].DataType);
//Check rows
DataRow row = table.Rows[0];
Assert.Equal(1, row[0]);
Assert.Equal("1", row[1]);
row = table.Rows[1];
Assert.Equal(2, row[0]);
Assert.Equal("2", row[1]);
row = table.Rows[2];
Assert.Equal(3, row[0]);
Assert.Equal("3", row[1]);
}
示例7: XmlTest20
public void XmlTest20()
{
MakeParentTable1();
using (FileStream stream = new FileStream(_tempFile, FileMode.Create))
{
_parentTable1.WriteXml(stream, XmlWriteMode.IgnoreSchema);
}
DataSet ds = new DataSet();
DataTable table = new DataTable("HelloWorldTable");
XmlReadMode mode = XmlReadMode.Auto;
table.Columns.Add(new DataColumn("id", typeof(int)));
ds.Tables.Add(table);
using (FileStream stream = new FileStream(_tempFile, FileMode.Open))
{
//ReadXml wont read anything as TableNames dont match
mode = table.ReadXml(stream);
}
Assert.Equal(XmlReadMode.IgnoreSchema, mode);
Assert.Equal("HelloWorldTable", table.TableName);
Assert.Equal(0, table.Rows.Count);
Assert.Equal(1, table.Columns.Count);
Assert.Equal(typeof(int), table.Columns[0].DataType);
Assert.Equal("System.Data.DataSet", table.DataSet.ToString());
Assert.Equal("NewDataSet", table.DataSet.DataSetName);
}
示例8: XmlTest30
public void XmlTest30()
{
MakeDummyTable();
using (FileStream stream = new FileStream(_tempFile, FileMode.Create))
{
_dummyTable.WriteXml(stream, XmlWriteMode.DiffGram);
}
Assert.Equal(3, _dummyTable.Rows.Count);
DataSet dataSet = new DataSet("HelloWorldDataSet");
DataTable table = new DataTable("DummyTable");
table.Columns.Add(new DataColumn("DummyItem", typeof(string)));
dataSet.Tables.Add(table);
//Call ReadXml on a table which belong to a DataSet
table.ReadXml(_tempFile);
Assert.Equal("HelloWorldDataSet", table.DataSet.DataSetName);
Assert.Equal(1, table.Columns.Count);
Assert.Equal(typeof(string), table.Columns[0].DataType);
Assert.Equal(3, table.Rows.Count);
//Check Rows
DataRow row = table.Rows[0];
Assert.Equal("DummyItem 1", row[0]);
Assert.Equal(DataRowState.Unchanged, row.RowState);
row = table.Rows[1];
Assert.Equal("Changed_DummyItem 2", row[0]);
Assert.Equal(DataRowState.Modified, row.RowState);
row = table.Rows[2];
Assert.Equal("DummyItem 3", row[0]);
Assert.Equal(DataRowState.Unchanged, row.RowState);
}
示例9: XmlTest15
public void XmlTest15()
{
MakeParentTable1();
using (FileStream stream = new FileStream(_tempFile, FileMode.Create))
{
_parentTable1.WriteXml(stream, XmlWriteMode.IgnoreSchema);
}
//Create a target table which has matching column(s) name and an extra column
DataTable table = new DataTable("ParentTable");
XmlReadMode mode = XmlReadMode.Auto;
table.Columns.Add(new DataColumn("id", typeof(int)));
table.Columns.Add(new DataColumn("ParentItem", typeof(string)));
table.Columns.Add(new DataColumn("DepartmentID", typeof(int)));
table.Columns.Add(new DataColumn("DummyColumn", typeof(string)));
using (FileStream stream = new FileStream(_tempFile, FileMode.Open))
{
mode = table.ReadXml(stream);
}
Assert.Equal(XmlReadMode.IgnoreSchema, mode);
Assert.Equal("ParentTable", table.TableName);
Assert.Equal(3, table.Rows.Count);
Assert.Equal(4, table.Columns.Count);
Assert.Null(table.DataSet);
//Check the Columns
Assert.Equal("id", table.Columns[0].ColumnName);
Assert.Equal(typeof(int), table.Columns[0].DataType);
Assert.Equal("ParentItem", table.Columns[1].ColumnName);
Assert.Equal(typeof(string), table.Columns[1].DataType);
Assert.Equal("DepartmentID", table.Columns[2].ColumnName);
Assert.Equal(typeof(int), table.Columns[2].DataType);
Assert.Equal("DummyColumn", table.Columns[3].ColumnName);
Assert.Equal(typeof(string), table.Columns[3].DataType);
//Check the rows
DataRow row = table.Rows[0];
Assert.Equal(1, row["id"]);
Assert.Equal("ParentItem 1", row["ParentItem"]);
Assert.Equal(1, row["DepartmentID"]);
Assert.Equal(DBNull.Value, row["DummyColumn"]);
row = table.Rows[1];
Assert.Equal(2, row["id"]);
Assert.Equal("ParentItem 2", row["ParentItem"]);
Assert.Equal(2, row["DepartmentID"]);
Assert.Equal(DBNull.Value, row["DummyColumn"]);
row = table.Rows[2];
Assert.Equal(3, row["id"]);
Assert.Equal("ParentItem 3", row["ParentItem"]);
Assert.Equal(3, row["DepartmentID"]);
Assert.Equal(DBNull.Value, row["DummyColumn"]);
}
示例10: XmlTest16
public void XmlTest16()
{
MakeParentTable1();
using (FileStream stream = new FileStream(_tempFile, FileMode.Create))
{
//Write the Xml with schema information
_parentTable1.WriteXml(stream, XmlWriteMode.WriteSchema);
}
XmlReadMode mode = XmlReadMode.Auto;
DataTable table = new DataTable();
table.Columns.Add(new DataColumn("id", typeof(int)));
using (FileStream stream = new FileStream(_tempFile, FileMode.Open))
{
mode = table.ReadXml(stream);
}
Assert.Equal(XmlReadMode.ReadSchema, mode);
Assert.Equal(string.Empty, table.TableName);
Assert.Equal(0, table.Rows.Count);
Assert.Equal(1, table.Columns.Count);
Assert.Equal(typeof(int), table.Columns[0].DataType);
Assert.Null(table.DataSet);
}
示例11: XmlTest14
public void XmlTest14()
{
MakeParentTable1();
using (FileStream stream = new FileStream(_tempFile, FileMode.Create))
{
_parentTable1.WriteXml(stream, XmlWriteMode.IgnoreSchema);
}
//Create a target table which has nomatching column(s) names
DataTable table = new DataTable("ParentTable");
XmlReadMode mode = XmlReadMode.Auto;
table.Columns.Add(new DataColumn("sid", typeof(string)));
using (FileStream stream = new FileStream(_tempFile, FileMode.Open))
{
//ReadXml does not read anything as the column names are not matching
mode = table.ReadXml(stream);
}
Assert.Equal(XmlReadMode.IgnoreSchema, mode);
Assert.Equal("ParentTable", table.TableName);
Assert.Equal(3, table.Rows.Count);
Assert.Equal(1, table.Columns.Count);
Assert.Equal("sid", table.Columns[0].ColumnName);
Assert.Equal(typeof(string), table.Columns[0].DataType);
Assert.Null(table.DataSet);
//Check the rows
foreach (DataRow row in table.Rows)
Assert.Equal(DBNull.Value, row[0]);
}
示例12: XmlTest13
public void XmlTest13()
{
MakeParentTable1();
using (FileStream stream = new FileStream(_tempFile, FileMode.Create))
{
_parentTable1.WriteXml(stream, XmlWriteMode.IgnoreSchema);
}
DataTable table = new DataTable("ParentTable");
XmlReadMode mode = XmlReadMode.Auto;
table.Columns.Add(new DataColumn("id", typeof(string)));
using (FileStream stream = new FileStream(_tempFile, FileMode.Open))
{
mode = table.ReadXml(stream);
}
Assert.Equal(XmlReadMode.IgnoreSchema, mode);
Assert.Equal("ParentTable", table.TableName);
Assert.Equal(3, table.Rows.Count);
Assert.Equal(1, table.Columns.Count);
Assert.Equal(typeof(string), table.Columns[0].DataType);
Assert.Null(table.DataSet);
//Check Rows
DataRow row = table.Rows[0];
Assert.Equal("1", row[0]);
row = table.Rows[1];
Assert.Equal("2", row[0]);
row = table.Rows[2];
Assert.Equal("3", row[0]);
}
示例13: XmlTest12
public void XmlTest12()
{
MakeParentTable1();
using (FileStream stream = new FileStream(_tempFile, FileMode.Create))
{
_parentTable1.WriteXml(stream, XmlWriteMode.IgnoreSchema);
}
DataTable table = new DataTable("Table1");
XmlReadMode mode = XmlReadMode.Auto;
table.Columns.Add(new DataColumn("id", typeof(int)));
using (FileStream stream = new FileStream(_tempFile, FileMode.Open))
{
//Same as last test. ReadXml does not read anything as table names dont match
mode = table.ReadXml(stream);
}
Assert.Equal(XmlReadMode.IgnoreSchema, mode);
Assert.Equal("Table1", table.TableName);
Assert.Equal(0, table.Rows.Count);
Assert.Equal(1, table.Columns.Count);
Assert.Equal(typeof(int), table.Columns[0].DataType);
Assert.Null(table.DataSet);
}
示例14: XmlTest28
public void XmlTest28()
{
MakeDummyTable();
using (FileStream stream = new FileStream(_tempFile, FileMode.Create))
{
_dummyTable.WriteXml(stream, XmlWriteMode.DiffGram);
}
//Create a table and set the table name
DataTable table = new DataTable("DummyTable");
//define the table schame partially with a column name which does not match with any
//table columns in the diffgram
table.Columns.Add(new DataColumn("WrongColumnName", Type.GetType("System.String")));
XmlReadMode mode = XmlReadMode.Auto;
using (FileStream stream = new FileStream(_tempFile, FileMode.Open))
{
mode = table.ReadXml(stream);
}
Assert.Equal(XmlReadMode.DiffGram, mode);
Assert.Null(table.DataSet);
Assert.Equal("DummyTable", table.TableName);
Assert.Equal(1, table.Columns.Count);
Assert.Equal(typeof(string), table.Columns[0].DataType);
Assert.Equal(3, table.Rows.Count);
foreach (DataRow row in table.Rows)
Assert.Same(DBNull.Value, row[0]);
}
示例15: XmlTest21
public void XmlTest21()
{
MakeParentTable1();
using (FileStream stream = new FileStream(_tempFile, FileMode.Create))
{
_parentTable1.WriteXml(stream, XmlWriteMode.IgnoreSchema);
}
DataSet ds = new DataSet();
DataTable table = new DataTable("ParentTable");
XmlReadMode mode = XmlReadMode.Auto;
table.Columns.Add(new DataColumn("id", Type.GetType("System.Int32")));
ds.Tables.Add(table);
using (FileStream stream = new FileStream(_tempFile, FileMode.Open))
{
mode = table.ReadXml(stream);
}
Assert.Equal(XmlReadMode.IgnoreSchema, mode);
Assert.Equal("ParentTable", table.TableName);
Assert.Equal(3, table.Rows.Count);
Assert.Equal(1, table.Columns.Count);
Assert.Equal(typeof(int), table.Columns[0].DataType);
Assert.Equal("System.Data.DataSet", table.DataSet.ToString());
Assert.Equal("NewDataSet", table.DataSet.DataSetName);
//Check the rows
DataRow row = table.Rows[0];
Assert.Equal(1, row[0]);
row = table.Rows[1];
Assert.Equal(2, row[0]);
row = table.Rows[2];
Assert.Equal(3, row[0]);
}