我有一个XML元素,其DateTimeOffset中的值为2099-12-31T23:59:58.576+13:00
。
运行myDS.ReadXml(myStream, XmlReadMode.InferTypedSchema)
会得到Specified cast is not valid.Couldn't store <2019-12-31 23:59:58> in MyColDTO Column. Expected type is DateTimeOffset.
附加的偏移量应清楚地指示DateTimeOffset而不是DateTime。
为什么DotNet(FW 4.6.2)不能正确识别它? 有简单的方法可以解决吗?
====自包含的vb.net单元测试===
<TestMethod()> Public Sub THC2398_ReadXml_Bug()
Dim ds1 As DataSet, ds2 As DataSet, dt1 As DataTable, dt2 As DataTable, dr As DataRow, dtoAt As DateTimeOffset = DateTimeOffset.Parse("2019-12-31T23:59:58.576+13:00")
Assert.AreEqual("v4.0.30319", GetType(String).Assembly.ImageRuntimeVersion, "Version check")
Assert.AreEqual("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", GetType(String).Assembly.FullName, "Version check")
ds1 = New DataSet("ds")
dt1 = New DataTable("dt")
ds1.Tables.Add(dt1)
dt1.Columns.Add(New DataColumn("MyColDTO", GetType(DateTimeOffset)))
dr = dt1.NewRow
dr("MyColDTO") = dtoAt
dt1.Rows.Add(dr)
Assert.AreEqual(1, dt1.Rows.Count)
Assert.AreEqual("DateTimeOffset", dt1.Rows(0)(0).GetType.Name)
Dim tw As IO.TextWriter = New StringWriter, tr As TextReader
dt1.WriteXml(tw, XmlWriteMode.IgnoreSchema)
System.Console.WriteLine(tw.ToString)
tr = New StringReader(tw.ToString)
ds2 = New DataSet("ds")
ds2.ReadXml(tr, XmlReadMode.InferTypedSchema)
dt2 = ds2.Tables("dt")
Assert.AreEqual(1, dt2.Rows.Count)
'Assert.AreEqual("DateTimeOffset", dt2.Rows(0)(0).GetType.Name, "this was expected")
Assert.AreEqual("DateTime", dt2.Rows(0)(0).GetType.Name, "*BUG* WHAT?! how can you tell me that 2099-12-31T23:59:58.576+13:00 is a DateTime?")
For Each item As DataRow In dt2.Rows
dr = dt1.NewRow
Try
dr.ItemArray = item.ItemArray
Catch ex As ArgumentException
Assert.AreEqual("Specified cast is not valid.Couldn't store <2019-12-31 23:59:58> in MyColDTO Column. Expected type is DateTimeOffset.", ex.Message, "Confirm that the bug is giving its bad message")
Assert.Fail("This should not have happened")
End Try
dt1.Rows.Add(dr)
Next
End Sub
控制台的输出:
<ds>
<dt>
<MyColDTO>2019-12-31T23:59:58.576+13:00</MyColDTO>
</dt>
</ds>
答案 0 :(得分:0)
答案是..因为数据集不支持DateTimeOffset DataColumn.DataType Property。但是.. SQL Server可以datetimeoffset (Transact-SQL)
似乎Microsoft软件与其自身不兼容。