C# DataTable 不支持来自 XML 的模式推断。?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1935522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 22:00:09  来源:igfitidea点击:

DataTable does not support schema inference from Xml.?

c#xmldatatable

提问by monkey_boys

<?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?

这我的xml文件有什么问题吗?

    bool CheckAdapterExist(string aid)
    {
        DataTable dt = new DataTable();
        dt.ReadXml(axml);
        MessageBox.Show(dt.Rows[0]["ID"].ToString());

        return true;
    }

采纳答案by Adriaan Stander

Try using a Datasetrather

尝试使用数据集而不是

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?

DataTable.ReadXml(filename) 引发错误。为什么?

回答by max

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];

回答by Aldo

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];

回答by Matthew Sharpe

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

如果你是写表的人,你可以通过在写表的同时写模式来解决这个问题。请参阅:http: //msdn.microsoft.com/en-us/library/ms135456.aspx

回答by Mark Ibanez

If you're reading an XMLfile from DataTable.WriteXml, make sure you include an XmlWriteMode.WriteSchema.

如果您XML从读取文件DataTable.WriteXml,请确保包含XmlWriteMode.WriteSchema.

Example:

例子:

Table.WriteXml(DataFilePath, XmlWriteMode.WriteSchema);