使用XmlSerializer序列化DataType =“time”字段

时间:2010-03-08 15:28:08

标签: c# .net xml xml-serialization

使用XmlSerializer序列化DateTime字段时,我得到一个奇怪的结果。

我有以下课程:

public class RecordExample
{
    [XmlElement("TheTime", DataType = "time")]
    public DateTime TheTime { get; set; }

    [XmlElement("TheDate", DataType = "date")]
    public DateTime TheDate { get; set; }

    public static bool Serialize(
        Stream stream, object obj, Type objType, Encoding encoding)
    {
        try
        {
            var settings = new XmlWriterSettings { Encoding = encoding };

            using (var writer = XmlWriter.Create(stream, settings))
            {
                var xmlSerializer = new XmlSerializer(objType);
                if (writer != null) xmlSerializer.Serialize(writer, obj);
            }

            return true;
        }
        catch (Exception)
        {
            return false;
        }
    }
}

当我调用XmlSerializer并使用以下测试代码时:

var obj = new RecordExample 
{ 
    TheDate = DateTime.Now.Date, 
    TheTime = new DateTime(0001, 1, 1, 12, 00, 00) 
};

var ms = new MemoryStream();

RecordExample.Serialize(ms, obj, typeof(RecordExample), Encoding.UTF8);
txtSource2.Text = Encoding.UTF8.GetString(ms.ToArray());

我得到了一些奇怪的结果,这里是生成的xml:

<?xml version="1.0" encoding="utf-8"?>
<RecordExample 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <TheTime>12:00:00.0000000+00:00</TheTime>
    <TheDate>2010-03-08</TheDate>
</RecordExample>

我知道如何让“TheTime”元素包含一个看起来更像这样的时间:

<TheTime>12:00:00.0Z</TheTime>

......那就是我所期待的?

由于

戴夫

4 个答案:

答案 0 :(得分:2)

我自己遇到了不同的问题......但是我试图序列化TimeSpan对象。解决方案是拥有两个属性,一个持有TimeSpan,另一个是TimeSpan的字符串表示,它被序列化。这是模式:

[XmlIgnore]
public TimeSpan ScheduledTime
{
    get;
    set;
}

[XmlElement("ScheduledTime", DataType="duration")]
public string XmlScheduledTime
{
    get { return XmlConvert.ToString(ScheduledTime); }
    set { ScheduledTime = XmlConvert.ToTimeSpan(value); }
}

但是,使用此代码,时间将按以下格式打印出来:

<ScheduledTime>PT23H30M</ScheduledTime>

The W3C definition of duration is here which explains it

答案 1 :(得分:1)

答案 2 :(得分:1)

扩展我对其他答案之一的评论。

public class RecordExample : IXmlSerializable
{
    public DateTime TheTime { get; set; }
    public DateTime TheDate { get; set; }

    public XmlSchema GetSchema()
    {
        return null;
    }

    public void ReadXml(XmlReader reader)
    {
        // TODO : Deserialization logic here
    }

    public void WriteXml(XmlWriter writer)
    {
        writer.WriteElementString(
            "date", 
            this.TheDate.ToString("yyyy-MM-dd"));

        writer.WriteElementString(
            "time", 
            this.TheTime.ToString("hh:mm:ss.fK"));
    }
}

像这样序列化:

var rc = new RecordExample()
{
    TheDate = DateTime.Today,
    TheTime = DateTime.UtcNow
};

var serializer = new XmlSerializer(typeof(RecordExample));

var ms = new MemoryStream();

serializer.Serialize(ms, rc);

ms.Seek(0, SeekOrigin.Begin);

Console.WriteLine(new StreamReader(ms).ReadToEnd());

输出示例:

<?xml version="1.0"?>
<RecordExample>
  <date>2010-03-08</date>
  <time>04:26:16.1Z</time>
</RecordExample>

答案 3 :(得分:1)

我同意其他答案(当他们弹出时我没有完成写作)。它看起来不像是直接的。 使用Reflector查看源代码显示时间值最终被转换为具有System.Xml.XmlConvert.ToString的字符串,其格式为:

HH:MM:ss.fffffffzzzzzz

因此有两个属性,真正的属性[XmlIgnore]和你自己构建的字符串是一个很好的方法。