编写一个空的XML文件 [英] Wirting an empty XML file

查看:96
本文介绍了编写一个空的XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设法修复了先前的代码,但是现在修复了它的编写过程,并使用了空的数据集来填充了空的XML文件,为什么?谢谢:) x


Ive managed to fix the previous code but now its writing and empty XML file with an empty dataset, any reason why? thanks :) x


private void databasexml_Click(object sender, EventArgs e)
     {



         string sql = "SELECT * FROM BookDetails";
         DataSet ds = new DataSet();
         BindingSource bs = new BindingSource();
         SqlDataAdapter sda = new SqlDataAdapter(sql, stringConnection);


         try
         {

         ds.WriteXml("C:\\Users\\Louise Tooze\\Desktop\\XMLFile2.xml");
         dataGridView1.DataMember = "";
         sda.Fill(ds);


         dataGridView1.DataSource = ds.Tables[0];
         dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
         dataGridView1.Update();
         sda.Update(ds);

         sda.Dispose();

         MessageBox.Show("XML File created Successfully");
         }

         catch (Exception ex)
         {
             MessageBox.Show(ex.Message);
         }

     }

推荐答案

您正在创建new DataSet ds,然后在其中没有任何内容之前使用WriteXml.尝试先填充它,然后再编写.
You are creating a new DataSet ds, then using WriteXml before there is anything in it. Try filling it first, then writing it.


将代码更改为:
Change your code to:
try
{
    .   
    .
    .
   
    // data filled in dataset
    sda.Fill(ds);
     
    // This line will be after you populate the dataset with data
    ds.WriteXml("C:\\Users\\Louise Tooze\\Desktop\\XMLFile2.xml");
    .
    .
    .
    .
    .
}


这篇关于编写一个空的XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆