日期:2014-05-17  浏览次数:20777 次

datagriedview按照其中某列值不同数据导出xml文件

问题一:如上图,根据运单号不同把DataGridView中的数据导出不同的xml文件中,文件名以该运单来命名,请问怎么实现?


问题二:通过数据库查询可实现对不同运单作分类后的运单号+xml,请问怎么将数据库中的xml格式的数据列用C#编程导出到xml文件中。查询结果如下图



说明,至于测试数据,大家可以随便写。

------解决方案--------------------

   //按id不同形成不同的文件,文件内容是XML
   public void writexml(string id,string xml)
     {
            
         StreamWriter sw =new StreamWriter(Application.StartupPath + @"\" + id + ".xml");
         try 
         {
            sw.WriteLine(xml);
            sw.Close();
         }
         catch (Exception ex )
         {
            MessageBox.Show(ex.Message);
         }   
     }
    private void button18_Click(object sender, EventArgs e)
    { 
        SqlConnection cnn=new SqlConnection();               
        cnn.ConnectionString = @"server=192.168.1.2;database=data;user id=sa;password=sa";
        cnn.Open();
        DataSet ds =new DataSet();        
        SqlDataAdapter da=new SqlDataAdapter("select * from table", cnn);        
        cnn.Close();
        da.Fill(ds, "table");        
        if(ds.Tables[0].Rows.Count==0)
        {            
            for(int i=0;i<ds.Tables[0].Rows.Count - 1;i++)
   &n