dataSet.GetXml()不为null或空列返回xml

时间:2011-02-04 22:43:46

标签: c# xml dataset xsd

当我调用dataSet.GetXml()时,我没有为具有null或空值的列返回任何xml。有一种简单有效的方法可以解决这个问题吗?以下问题的一个例子。请注意第二个结果部分中缺少a2的方法。

<results>
<a1>test1</a1>
<a2>test2</a2>
<a3>test3</a3>
</results>
<results>
<a1>Atest1</a1>
<a3>Atest3</a3>
</results>

4 个答案:

答案 0 :(得分:4)

此Microsoft知识库文章中详细说明了此问题:http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q317961。有关更多详细信息,请参阅此前的SO问题:DataSet.GetXml not returning null results

我认为你的直接问题没有一个好的解决方案。鉴于上下文,可能有另一种方法可以解决问题。

答案 1 :(得分:3)

一种对我有用的解决方案。

首先克隆DataTable,使所有列都为string类型,用string.empty替换所有空值,然后在新数据集上调用GetXml

        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();

答案 2 :(得分:2)

我一直在寻找使用DataSet.WriteXML()将空字段写入XML的解决方案。我发现以下是以性能优化的方式工作。为方便起见,我创建了一个功能。通过调用以下函数并替换表格,一个接一个地更改数据集表。

    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;
    }

答案 3 :(得分:0)

            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count == 0)
            {
                foreach (DataTable dt in ds.Tables)
                {
                    foreach (DataColumn dc in dt.Columns)
                    {
                        dc.DataType = typeof(String);
                    }
                }

                DataRow dr = ds.Tables[0].NewRow();
                for (int i = 0; i < dr.ItemArray.Count(); i++)
                {
                    dr[i] = string.Empty;
                }
                ds.Tables[0].Rows.Add(dr);
            }