将DataSet序列化为XML-缺少具有空值的列

时间:2018-09-30 20:37:45

标签: c# xml datatable dataset

我正在将新系统与旧系统集成,部分要求是以XML格式写入缓存数据。我已经对旧系统进行了反编译,并设法尽可能地接近需求数据格式。

这是旧版系统的作用:

DataSet formData = getDatasetValuesSomehow();

using (StringWriter stringWriter = new StringWriter())
{
    formData.WriteXml((TextWriter) stringWriter, XmlWriteMode.DiffGram);
    stringWriter.Flush();
}

输出如下:

<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
    <NewDataSet>
        <VALUES diffgr:id="VALUES1" msdata:rowOrder="0" diffgr:hasChanges="inserted">
            <DEBUG_ID>9141c23e-1149-4ba2-b5d1-5c8c77561f0d</DEBUG_ID>
            <DATECREATED>2018-09-24T15:28:00.3769504+01:00</DATECREATED>
            <DATELASTMODIFIED>2018-09-24T15:28:00.3769504+01:00</DATELASTMODIFIED>
            <CREATEDBY>TRAILMAX</CREATEDBY>
            <LASTMODIFIEDBY>TRAILMAX</LASTMODIFIEDBY>
            <TEXT>Oh MY!</TEXT>
            <DATE>2018-09-24T00:00:00+01:00</DATE>
            <INTEGER>42</INTEGER>
            <KEY1 />
        </VALUES>
    </NewDataSet>
</diffgr:diffgram>

代表数据库表行中的所有值。请注意<KEY1 />-这是一个带有null值的字段。

当我做同样的事情时(关于XML的获取方式),我只得到其中包含值的字段,没有空字段:

<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
  <NewDataSet>
    <VALUES diffgr:id="VALUES1" msdata:rowOrder="0" diffgr:hasChanges="inserted">
      <DEBUG_ID>32ac8a83-a45b-473c-9890-d47a4b274426</DEBUG_ID>
      <TEXT>Hello world</TEXT>
      <DATE>2018-09-30T15:16:13.63+01:00</DATE>
    </VALUES>
  </NewDataSet>
</diffgr:diffgram>

但是表架构是相同的,在这里我还应该看到<KEY1 /><INTEGER />等字段。

这是我的C#:

[Test]
public void Another_Test_ExpectedBehaviour()
{
    var resultDataSet = new DataSet();
    DataTable values = resultDataSet.Tables.Add("VALUES");
    DataRow valuesRow = values.NewRow();
    var recordId = Guid.NewGuid(); // that's just dummy here - I have a value coming from somewhere
    using (var sqlConnection = new SqlConnection(ConfigurationContext.Current.GetHrnetConnectionstring()))
    {
        sqlConnection.Open();
        var sqlCommand = new SqlCommand("SELECT top 1 * from DEBUG where DEBUG_ID = @1", sqlConnection);
        sqlCommand.Parameters.AddWithValue("@1", recordId);
        using (SqlDataAdapter dataAdapter = new SqlDataAdapter(sqlCommand))
        {
            var dataTable = new DataTable();
            dataAdapter.Fill(dataTable);

            var dataRow = dataTable.Rows[0];
            foreach (DataColumn column in dataTable.Columns)
            {
                string columnName = column.ColumnName;
                object columnValue = RuntimeHelpers.GetObjectValue(dataRow[columnName, DataRowVersion.Default]);

                values.Columns.Add(columnName, column.DataType);
                valuesRow[columnName] = columnValue;
            }
        }
    }
    values.Rows.Add(valuesRow);

    // Now get XML
    using (StringWriter stringWriter = new StringWriter())
    {
        resultDataSet.WriteXml(stringWriter, XmlWriteMode.DiffGram);
        stringWriter.Flush();
        var xml = stringWriter.ToString();
    }
}

当我调试并查看resultDataSet的最终状态时,可以看到所有列均可用: All the columns are available int the list of columns when inspected in debug

但是只有那些具有值的才可以进入XML。反序列化后如何显示XML中的空列? 编辑:这是旧版系统代码:https://gist.github.com/trailmax/0100bd6926f311fae277848ce7624e98-我看不到我在做什么,但他们得到的是空列,而我没有。只有它们在.Net 3.5上,而我在4.7.1上

0 个答案:

没有答案