2013-05-30 67 views
2

我有下面的代碼,其中從3個表中取出數據並寫入一個xml。
我想要寫入(當一個記錄列有null值)xml列上的空值。例如,如果(Category_name == Null)寫入xml(Null)現在代碼跳過列,甚至沒有這個列在xml上。數據集爲xml空值

string xmlFileData = ""; 

    string[] tables = new string[] { "category", "company", "config" }; 
    string query; 
    xmlFileData += "<MyXml>"; 
    SqlConnection conn; 
    dbconnect obj; 
    obj = new dbconnect();//initailizing class object 
    for (int i = 0; i < tables.Length; i++) 
    { 
     string ifemptquery; 
     DataSet ds = new DataSet(); 

     DataSet ds1 = new DataSet(); 
     conn = obj.getConnection(); //calling connection function 

     ifemptquery = "SELECT * FROM " + tables[i] "; 
     SqlCommand cmd1 = new SqlCommand(ifemptquery, conn); 
     conn.Open(); 
     SqlDataAdapter da1 = new SqlDataAdapter(cmd1); 
     DataTable dt1 = new DataTable(); 
     da1.Fill(dt1); 
     conn.Close(); 
     if (dt1.Rows.Count > 0) 
     { 
      query = "SELECT * FROM " + tables[i] "; 
      SqlCommand cmd = new SqlCommand(query, conn); 
      conn.Open(); 
      SqlDataAdapter da = new SqlDataAdapter(cmd); 
      da.Fill(ds); 
      conn.Close(); 
      conn.Dispose(); 
      ds.DataSetName = tables[i]; 
      string vartbname = tables[i]; 
      string trimed_tbname = vartbname.Replace("_", ""); 
      ds.Tables[0].TableName = trimed_tbname; 
      xmlFileData += ds.GetXml(); 
     } 
     else 
     { 

     } 


    } 
    xmlFileData += "</MyXml>"; 
    File.WriteAllText(Server.MapPath("~/xmlbackup/") + "Backup.xml", xmlFileData); 

回答

0

我一直在尋找一個使用DataSet.WriteXML()將空字段寫入XML的解決方案。 Vlad發佈的答案是我在項目中使用的答案,但我發現以下更多的性能優化方式。爲了您的方便,我創建了一個函數。通過調用以下函數並替換表格,一個接一個地更改數據集表。

private DataTable GetNullFilledDataTableForXML(DataTable dtSource) 
{ 
    // Create a target table with same structure as source and fields as strings 
    // We can change the column datatype as long as there is no data loaded 
    DataTable dtTarget = dtSource.Clone(); 
    foreach (DataColumn col in dtTarget.Columns) 
     col.DataType = typeof(string); 

    // Start importing the source into target by ItemArray copying which 
    // is found to be reasonably fast for nulk operations. VS 2015 is reporting 
    // 500-525 milliseconds for loading 100,000 records x 10 columns 
    // after null conversion in every cell which may be usable in many 
    // circumstances. 
    // Machine config: i5 2nd Gen, 8 GB RAM, Windows 7 64bit, VS 2015 Update 1 
    int colCountInTarget = dtTarget.Columns.Count; 
    foreach (DataRow sourceRow in dtSource.Rows) 
    { 
     // Get a new row loaded with data from source row 
     DataRow targetRow = dtTarget.NewRow(); 
     targetRow.ItemArray = sourceRow.ItemArray; 

     // Update DBNull.Values to empty string in the new (target) row 
     // We can safely assign empty string since the target table columns 
     // are all of string type 
     for (int ctr = 0; ctr < colCountInTarget; ctr++) 
      if (targetRow[ctr] == DBNull.Value) 
       targetRow[ctr] = String.Empty; 

     // Now add the null filled row to target datatable 
     dtTarget.Rows.Add(targetRow); 
    } 

    // Return the target datatable 
    return dtTarget; 
}