开发者

Serializing a DataType="time" field using XmlSerializer

I'm getting an odd result when serializing a DateTime field using XmlSerializer.

I have the following class:

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;
        }
    }
}

When i call the use the XmlSerializer with the following testing code:

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());

I get some strange results, here's the xml that is produced:

<?xml version="1.0" encoding="utf-8"?>
<RecordExample 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:xsd="http://w开发者_StackOverflow中文版ww.w3.org/2001/XMLSchema">
    <TheTime>12:00:00.0000000+00:00</TheTime>
    <TheDate>2010-03-08</TheDate>
</RecordExample>

Any idea's how i can get the "TheTime" element to contain a time which looks more like this:

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

...as that's what i was expecting?

Thanks

Dave


I've had different issues with this myself... however I was attempting to serialize a TimeSpan object. The solution was to have two properties, one that held the TimeSpan, and one that was a string representation of the TimeSpan which got Serialized. Here was the pattern:

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

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

However, with this code, the time is printed out in the following format:

<ScheduledTime>PT23H30M</ScheduledTime>

The W3C definition of duration is here which explains it.


take a look at this question Serializing DateTime to time without milliseconds and gmt


Expanding on the comment I made on one of the others answers.

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"));
    }
}

Serializing like this:

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());

Output example:

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


I concur with the other answers (I was not done writing when they popped up). it does not look like it is possible, in a direct way. A look at the source with Reflector shows that a time value ends up being converted to a string with the System.Xml.XmlConvert.ToString, that has a hard-coded format of:

HH:mm:ss.fffffffzzzzzz

So having two properties, the real one being [XmlIgnore] and a string that you build yourself is a good way to go.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜