dataSet.GetXml() doesn't return xml for null or blank columns

后端 未结 4 950
名媛妹妹
名媛妹妹 2021-01-06 00:51

When I call dataSet.GetXml() I don\'t get any xml returned for columns with null or blank values. Is there a simple, efficient way to get around this? An example of the prob

4条回答
  •  庸人自扰
    2021-01-06 01:48

    One solution that worked for me.

    First clone the DataTable, make all columns of type string, replace all null values with string.empty, then call GetXml on a new DataSet.

            DataTable dtCloned = dt.Clone();
            foreach (DataColumn dc in dtCloned.Columns)
                dc.DataType = typeof(string);
            foreach (DataRow row in dt.Rows)
            {
                dtCloned.ImportRow(row);
            }
    
            foreach (DataRow row in dtCloned.Rows)
            {
                for (int i = 0; i < dtCloned.Columns.Count; i++)
                {
                    dtCloned.Columns[i].ReadOnly = false;
    
                    if (string.IsNullOrEmpty(row[i].ToString()))
                        row[i] = string.Empty;
                }
            }
    
            DataSet ds = new DataSet();
            ds.Tables.Add(dtCloned);
            string xml = ds.GetXml();
    

提交回复
热议问题