【问题标题】:Handling Null in WriteXml在 WriteXml 中处理 Null
【发布时间】:2011-01-11 07:13:15
【问题描述】:

为了将 Linq 转换为 DataTable,我使用以下扩展方法(取自 Stackoverflow)

Linq 到数据表

public static DataTable ToDataTable<T>(this IEnumerable<T> items)
 {
      DataTable table = new DataTable(typeof(T).Name);
      PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public |
                                                    BindingFlags.Instance);
      foreach (var prop in props)
        {
           Type propType = prop.PropertyType;
           // Is it a nullable type? Get the underlying type 
           if (propType.IsGenericType && 
               propType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
               propType = new NullableConverter(propType).UnderlyingType;
                table.Columns.Add(prop.Name, propType);
            }

            foreach (var item in items)
            {
                var values = new object[props.Length];
                for (var i = 0; i < props.Length; i++)
                        values[i] = props[i].GetValue(item, null);
                table.Rows.Add(values);
            }
            return table;
        }

WriteXml

    PersonDB.PersonDataContext con = new PersonDB.PersonDataContext();
    DataTable tb = new DataTable();
    tb = con.Persons.ToDataTable();
    tb.WriteXml(@"d:\temp\Person.xml");

问题


扩展方法创建 XML 文件,但对于空值,不会在 XML 文件中创建任何元素。它表示如果 Commission 字段为 null,则 Xml 生成中缺少 Commission 元素。

我想为空值(参考类型)和 (0.00) 为小数和 (0) 为整数插入带有空字符串的元素。我需要在哪里进行更改?

【问题讨论】:

    标签: asp.net xml linq reflection datatable


    【解决方案1】:

    我会为 DataTable 创建一个扩展方法:

    public static DataTable ZeroNullValues(this DataTable dataTable)
    {
        foreach(DataRow row in dataTable.Rows)
        {
            for(int i=0;i<dataTable.Columns.Count;i++)
            {
                if(row[i] == null)
                {
                    Type columnType = dataTable.Columns[i].DataType;
                    if(columnType == typeof(string))
                    {
                        row[i] = string.Empty;
                    }
                    else if(columnType == typeof(int) || columnType == typeof(long))
                    {
                        row[i] = 0;
                    }
                    else if(columnType == typeof(float) || columnType == typeof(double))
                    {
                        row[i] = 0.00F;
                    }
                }
            }
        }
        return dataTable;
    }
    

    【讨论】:

      【解决方案2】:

      正如我之前回答的here

      您可以使用 WriteXmlSchema(和 ReadXmlSchema)来存储表的架构。这意味着虽然 NULL 列不包含在 XML 中,但它们仍然可以正确导入。

      using (SqlCommand sqlCommand = new SqlCommand(QUERY, CONNECTION))
      {
          SqlDataAdapter dataAdapter = new SqlDataAdapter(sqlCommand);
          DataSet dataSet = new DataSet();
          dataAdapter.Fill(dataSet);
          dataSet.WriteXmlSchema(@"C:\TEMP\Schema.xsd");
          dataSet.Tables[0].WriteXml(@"C:\TEMP\Output.xml");
      }
      

      这里有一篇非常有用的文章:Save Dataset to XML With Null Values

      【讨论】:

        猜你喜欢
        • 2014-06-19
        • 2017-02-08
        • 1970-01-01
        • 2023-03-12
        • 1970-01-01
        • 2014-06-07
        • 1970-01-01
        • 1970-01-01
        • 2012-10-08
        相关资源
        最近更新 更多