C# datatable 读取xml文件

时间:2021-04-06 12:04:29
我要做个小程序 需要将一个xml文件导入到datatable中,
我一开始用将sql中 需要的数据导入到datatable中,在将这些表在写入到几个
xml文件。在将这些文件考被到其他机器,但是在次将这些xml文件写入到新的
tabletable中就不行。
写入xml文件
private DataSet getDataset()
        {
            string sqlConn = "Data Source=localhost;Initial Catalog=test;Persist Security Info=True;User ID=sa;Password=picc4404";
            string sqldsz = "select * from dsz";           
            SqlDataAdapter dszdata = new SqlDataAdapter(sqldsz.ToString(), sqlConn);            
            DataSet ds = new DataSet();
            dszdata.Fill(ds, "dsz");            
            return ds;
        }
        
  private void button1_Click(object sender, EventArgs e)
        {
            //datatable 写入到xml
            DataSet baofei = new DataSet();
            baofei = getDataset();
            baofei.Tables["dsz"].WriteXml("dsz.xml");
            }
 再将这个xml文件拷贝的其他机器,datatable 读入xml
  private void button1_Click(object sender, EventArgs e)
 {
             //xml写入datatable
            DataSet baofeiread = new DataSet();
            DataTable daxread = new DataTable("daxread");
            baofeiread.Tables.Add(daxread);
            baofeiread.Tables["daxread"].ReadXml("dsz.xml");}
这样报错。
有什么办法能读取到呢?
xml文件:
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
  <dsz>
    <clausetype>U01</clausetype>
    <seats>5</seats>
    <touns>0</touns>
    <amount>50000</amount>
    <premium>638</premium>
  </dsz>
  <dsz>
    <clausetype>U01</clausetype>
    <seats>6</seats>
    <touns>0</touns>
    <amount>50000</amount>
    <premium>590</premium>
  </dsz>
  <dsz>
    <clausetype>U01</clausetype>
    <seats>10</seats>
    <touns>0</touns>
    <amount>50000</amount>
    <premium>590</premium>
  </dsz>
  </NewDataSet>

10 个解决方案

#1


应该是没有把框架写进去吧

#2


http://sunxitao88.blog.163.com/blog/static/683144392011416995716/

#3


WriteXmlSchema加上去就ok。

#4


  DataTable dt = new DataTable();
  dt.ReadXml("t.xml");  

如果直接读取会提示:DataTable 不支持来自 XML 的架构推断。
读取方法:


            DataTable dt = new DataTable("dsz");
            dt.Columns.Add("clausetype");
            dt.Columns.Add("seats");
            dt.Columns.Add("touns");           
            dt.Columns.Add("amount");
            dt.Columns.Add("premium");
            dt.ReadXml("t.xml");  


#5


是用读取架构后 还是提示 DataTable“daxread”与源中的任意 DataTable 都不匹配。 错误
   //写入架构
            baofei.Tables["dsz"].WriteXmlSchema("dszjg.xml");
            
                //写入数据
            baofei.Tables["dsz"].WriteXml("dsz.xml");
           
            //xml写入datatable
            DataSet baofeiread = new DataSet();
            DataTable daxread = new DataTable("daxread");
            
            baofeiread.Tables.Add(daxread);
            baofeiread.Tables["daxread"].ReadXmlSchema("dszjg.xml");
            baofeiread.Tables["daxread"].ReadXml("dsz.xml");

#6


引用 4 楼 wind_cloud2011 的回复:
  DataTable dt = new DataTable();
  dt.ReadXml("t.xml");  

如果直接读取会提示:DataTable 不支持来自 XML 的架构推断。
读取方法:


            DataTable dt = new DataTable("dsz");
            dt.Columns.Add("clausetype");
            dt.Columns.Add("seats");
            dt.Columns.Add("touns");           
            dt.Columns.Add("amount");
            dt.Columns.Add("premium");
            dt.ReadXml("t.xml");  




使用这个办法 是没有报错  但是导入的数据是空的:
 //xml写入datatable
            DataSet baofeiread = new DataSet();
            DataTable daxread = new DataTable("daxread");
            daxread.Columns.Add("clausetype", typeof(String));
            daxread.Columns.Add("seats", typeof(Double));
            daxread.Columns.Add("touns", typeof(Double));
            daxread.Columns.Add("amount", typeof(Double));
            daxread.Columns.Add("premium", typeof(Double));
            baofeiread.Tables.Add(daxread);
           string dr = daxread.Rows[0][0].ToString(); 
这一步就报错 在位置 0 处没有任何行。

#7


刚才的遗漏了 baofeiread.Tables["daxread"].ReadXml("dsz.xml");

#8


      DataTable daxread = new DataTable("daxread");
-->      DataTable dt = new DataTable("dsz");
你XML里是DSZ,不是daxread
 <dsz>
    <clausetype>U01</clausetype>
    <seats>5</seats>
    <touns>0</touns>
    <amount>50000</amount>
    <premium>638</premium>
  </dsz>

#9



            DataTable daxread = new DataTable("dsz");
            daxread.Columns.Add("clausetype");
            daxread.Columns.Add("seats");
            daxread.Columns.Add("touns");
            daxread.Columns.Add("amount");
            daxread.Columns.Add("premium");
            daxread.ReadXml("t.xml");
            string dr = daxread.Rows[0][0].ToString(); 

#10


用你的:

 DataSet baofeiread = new DataSet();
            DataTable daxread = new DataTable("dsz");
            daxread.Columns.Add("clausetype");
            daxread.Columns.Add("seats");
            daxread.Columns.Add("touns");
            daxread.Columns.Add("amount");
            daxread.Columns.Add("premium");
            baofeiread.Tables.Add(daxread);
            baofeiread.Tables[0].ReadXml("t.xml"); 
            //daxread.ReadXml("t.xml");
            string dr = baofeiread.Tables[0].Rows[0][0].ToString(); 

#1


应该是没有把框架写进去吧

#2


http://sunxitao88.blog.163.com/blog/static/683144392011416995716/

#3


WriteXmlSchema加上去就ok。

#4


  DataTable dt = new DataTable();
  dt.ReadXml("t.xml");  

如果直接读取会提示:DataTable 不支持来自 XML 的架构推断。
读取方法:


            DataTable dt = new DataTable("dsz");
            dt.Columns.Add("clausetype");
            dt.Columns.Add("seats");
            dt.Columns.Add("touns");           
            dt.Columns.Add("amount");
            dt.Columns.Add("premium");
            dt.ReadXml("t.xml");  


#5


是用读取架构后 还是提示 DataTable“daxread”与源中的任意 DataTable 都不匹配。 错误
   //写入架构
            baofei.Tables["dsz"].WriteXmlSchema("dszjg.xml");
            
                //写入数据
            baofei.Tables["dsz"].WriteXml("dsz.xml");
           
            //xml写入datatable
            DataSet baofeiread = new DataSet();
            DataTable daxread = new DataTable("daxread");
            
            baofeiread.Tables.Add(daxread);
            baofeiread.Tables["daxread"].ReadXmlSchema("dszjg.xml");
            baofeiread.Tables["daxread"].ReadXml("dsz.xml");

#6


引用 4 楼 wind_cloud2011 的回复:
  DataTable dt = new DataTable();
  dt.ReadXml("t.xml");  

如果直接读取会提示:DataTable 不支持来自 XML 的架构推断。
读取方法:


            DataTable dt = new DataTable("dsz");
            dt.Columns.Add("clausetype");
            dt.Columns.Add("seats");
            dt.Columns.Add("touns");           
            dt.Columns.Add("amount");
            dt.Columns.Add("premium");
            dt.ReadXml("t.xml");  




使用这个办法 是没有报错  但是导入的数据是空的:
 //xml写入datatable
            DataSet baofeiread = new DataSet();
            DataTable daxread = new DataTable("daxread");
            daxread.Columns.Add("clausetype", typeof(String));
            daxread.Columns.Add("seats", typeof(Double));
            daxread.Columns.Add("touns", typeof(Double));
            daxread.Columns.Add("amount", typeof(Double));
            daxread.Columns.Add("premium", typeof(Double));
            baofeiread.Tables.Add(daxread);
           string dr = daxread.Rows[0][0].ToString(); 
这一步就报错 在位置 0 处没有任何行。

#7


刚才的遗漏了 baofeiread.Tables["daxread"].ReadXml("dsz.xml");

#8


      DataTable daxread = new DataTable("daxread");
-->      DataTable dt = new DataTable("dsz");
你XML里是DSZ,不是daxread
 <dsz>
    <clausetype>U01</clausetype>
    <seats>5</seats>
    <touns>0</touns>
    <amount>50000</amount>
    <premium>638</premium>
  </dsz>

#9



            DataTable daxread = new DataTable("dsz");
            daxread.Columns.Add("clausetype");
            daxread.Columns.Add("seats");
            daxread.Columns.Add("touns");
            daxread.Columns.Add("amount");
            daxread.Columns.Add("premium");
            daxread.ReadXml("t.xml");
            string dr = daxread.Rows[0][0].ToString(); 

#10


用你的:

 DataSet baofeiread = new DataSet();
            DataTable daxread = new DataTable("dsz");
            daxread.Columns.Add("clausetype");
            daxread.Columns.Add("seats");
            daxread.Columns.Add("touns");
            daxread.Columns.Add("amount");
            daxread.Columns.Add("premium");
            baofeiread.Tables.Add(daxread);
            baofeiread.Tables[0].ReadXml("t.xml"); 
            //daxread.ReadXml("t.xml");
            string dr = baofeiread.Tables[0].Rows[0][0].ToString();