【问题标题】:dataSet.GetXml() doesn't return xml for null or blank columnsdataSet.GetXml() 不为空列或空列返回 xml
【发布时间】:2011-06-21 16:17:28
【问题描述】:

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

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

【问题讨论】:

  • you 会为 null 输入什么值?也许考虑给列一个默认值?
  • 我只想将值设为空白。我希望有一种方法可以获取所有 xml 架构信息,而无需显式设置列默认值,或者通过 xml 进行任何类型的循环,因为我必须为许多不同的结果集执行此操作。
  • 空白和null不是一回事

标签: c# xml dataset xsd


【解决方案1】:

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

我认为您的直接问题没有很好的解决方案。不过,考虑到上下文,可能还有另一种解决问题的方法。

【讨论】:

    【解决方案2】:

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

    首先克隆 DataTable,将所有列设为字符串类型,将所有空值替换为 string.empty,然后在新的 DataSet 上调用 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();
    

    【讨论】:

    • 一次编辑:如果 dtCloned.Rows 为零,那么我们应该添加 -> if (dtCloned.Rows.Count == 0) dtCloned.Rows.Add();
    【解决方案3】:
                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);
                }
    

    【讨论】:

    • 您肯定需要对此答案添加一些解释,以告诉我们您为什么将其添加到已回答的问题中
    【解决方案4】:

    我一直在寻找使用 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;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-27
      • 1970-01-01
      相关资源
      最近更新 更多