DataTable does not support schema inference from Xml.?
<?xml version="1.0" encoding="utf-8"?>
<PHP_Adapter>
<Adapter>
<ID>11</ID>
<Provider>22</Provider>
<Connectstring>33</Connectstring>
</Adapter>
</PHP_Adapter>
This my Xml file what wrong?
bool CheckAdapterExist(string aid)
{
DataTable dt = new DataTab开发者_运维百科le();
dt.ReadXml(axml);
MessageBox.Show(dt.Rows[0]["ID"].ToString());
return true;
}
Try using a Dataset rather
DataSet ds = new DataSet();
ds.ReadXml(@"d:\test.xml");
MessageBox.Show(ds.Tables[0].Rows[0]["ID"].ToString());
Found at
DataTable.ReadXml(filename) throws an error. Why?
If you're reading an XML
file from DataTable.WriteXml
, make sure you include an XmlWriteMode.WriteSchema
.
Example:
Table.WriteXml(DataFilePath, XmlWriteMode.WriteSchema);
This works
string XML = @"
<MyTable>
<MyRecord>
<Col_1>test</Col_1>
<Col_2>1234</Col_2>
</MyRecord>
<MyRecord>
<Col_1>Record 2</Col_1>
<Col_2>2</Col_2>
</MyRecord>
</MyTable>
";
DataSet DS = new DataSet();
DS.ReadXml(new StringReader(XML));
DataTable DT=DS.Tables[0];
If you are the one writing the table, you can solve this problem by writing the schema at the same time as the table. See: http://msdn.microsoft.com/en-us/library/ms135456.aspx
Try this, this will work :
System.Xml.XmlTextReader reader =
new System.Xml.XmlTextReader(@"C:\Users\Mayank\Documents\Projects\XMLTEST\XMLTEST\XMLFile1.xml");
DataSet newTable = new DataSet();
newTable.ReadXml(reader);
DataTable _dt=newTable.Tables[0];
精彩评论