简体   繁体   English

具有DateTime元素的类型化XML列-DateTime被转换

[英]typed XML column with DateTime element - DateTime gets converted

I have a typed XML column in which I store the serialization from a C# object with a DateTime property. 我有一个类型化的XML列,在其中存储了带有DateTime属性的C#对象的序列化。 That DateTime is of Local type when I store it in the DB - although the serialized XML shows the time with an offset to GT time, as in '2009-09-22T13:52:32.2346252-07:00' (I live in Oregon). 当我将它存储在数据库中时,该DateTime是Local类型的-尽管序列化的XML显示的时间与GT时间存在偏移,如“ 2009-09-22T13:52:32.2346252-07:00”(我住在俄勒冈州) )。 When I read the table (from within the SQL Management Studio), the XML shows the DateTime object in UTC type. 当我从SQL Management Studio中读取表时,XML以UTC类型显示DateTime对象。 Is there a way to control this behavior? 有没有办法控制这种行为? It is important to me to respect the timezone of the data I put in, since it can come from different sources, and I need to respect that when I pull it out. 对我来说,尊重输入数据的时区非常重要,因为它可能来自不同的来源,并且在拔出数据时也需要尊重它。 I also do not want to convert every date time field from UTC to local after I read the objects from the DB. 从数据库读取对象后,我也不想将每个日期时间字段从UTC转换为本地。

this is the C# definition of the object I store: 这是我存储的对象的C#定义:

[Serializable]
[XmlType(Namespace="urn://MySchema")]
public class MyObject
{
    public DateTime RecordTime { get; set; }
    // .. omitted for clarity
}

This is the definition of the table (simplified for clarity) 这是表格的定义(为清楚起见已简化)

CREATE TABLE MyTable(
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [MyKey] [int] NULL,
    [TheObject] [xml](CONTENT [dbo].[MySchema]) NULL,
 CONSTRAINT [PK_MyTable] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF,       ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

This is how my sproc looks like (also simplified) 这就是我的存储库的外观(也简化了)

ALTER PROCEDURE [dbo].[SaveMyObjects] 
    @where int, 
    @object xml
AS
BEGIN
INSERT INTO MyTable
    (MyKey, TheObject)
    VALUES(@where, @object)
END

I need to do 2 things differently: 我需要以不同的方式做两件事:

  1. I want to control the format of the date string in the serialized XML - I'd prefer an ISO format for that time string 我想控制序列化XML中日期字符串的格式-我希望该时间字符串使用ISO格式
  2. I do not want to the DateTime to be converted to UTC when I store it in the DB, but I'd like the convenience of keeping it as a DateTime field. 当我将DateTime存储在数据库中时,我不希望将DateTime转换为UTC,但我希望将其保留为DateTime字段很方便。

How do I do that? 我怎么做? Any idea, pointer, link to some god's blog showing my my errors, etc.. would be much appreciated. 任何想法,指针,指向某个神的博客的链接(显示我的错误等等)都将不胜感激。

Not the cleanest way to do it, but you could do something like this: 这不是最干净的方法,但是您可以执行以下操作:

[Serializable]
[XmlType(Namespace="urn://MySchema")]
public class MyObject
{
    [XmlIgnore]
    public DateTime RecordTime { get; set; }

    //property for serialization purposes
    [XmlElement(ElementName="RecordTime")]
    public string XmlRecordTime
    {
        get { return this.RecordTime.ToString("o"); }
        set { this.RecordTime = new DateTime.Parse(value); }
    }
}

Also, take a look at XmlAttributeOverrides class - more work, but cleaner. 另外,看看XmlAttributeOverrides类-更多的工作,但是更干净。

是否可以使用DateTimeOffset代替DateTime?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM