日期:2014-05-18  浏览次数:20980 次

为什么用datatable的readXml无法获取相应的数据
写是用DT.WriteXml(fileName);并且写成功,相应位置生成对应文件了

string fileName = @System.Windows.Forms.Application.StartupPath + "\\data.xml";
  if (System.IO.File.Exists(fileName))
  {
  DT.ReadXml(fileName);
   

  MessageBox.Show(DT.Rows.Count.ToString());
  }  

可是怎么也读不出来,记录数始终为0

------解决方案--------------------
private static void DemonstrateReadWriteXMLDocumentWithString()
{
DataTable table = CreateTestTable("XmlDemo");
PrintValues(table, "Original table");

string fileName = "C:\\TestData.xml";
table.WriteXml(fileName, XmlWriteMode.WriteSchema);

DataTable newTable = new DataTable();
newTable.ReadXml(fileName);

// Print out values in the table.
PrintValues(newTable, "New table");
}

private static DataTable CreateTestTable(string tableName)
{
// Create a test DataTable with two columns and a few rows.
DataTable table = new DataTable(tableName);
DataColumn column = new DataColumn("id", typeof(System.Int32));
column.AutoIncrement = true;
table.Columns.Add(column);

column = new DataColumn("item", typeof(System.String));
table.Columns.Add(column);

// Add ten rows.
DataRow row;
for (int i = 0; i <= 9; i++)
{
row = table.NewRow();
row["item"] = "item " + i;
table.Rows.Add(row);
}

table.AcceptChanges();
return table;
}

private static void PrintValues(DataTable table, string label)
{
Console.WriteLine(label);
foreach (DataRow row in table.Rows)
{
foreach (DataColumn column in table.Columns)
{
Console.Write("\t{0}", row[column]);
}
Console.WriteLine();
}
}
------解决方案--------------------
XML结构是否符合DataTable dt = new DataTable("User");
DataColumn dc1 = new DataColumn("ID", Type.GetType("System.Int32"));
DataColumn dc2 = new DataColumn("FName", Type.GetType("System.String"));
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
System.IO.StringReader reader = new System.IO.StringReader(tbxml);
dt.ReadXml(reader);