我怎样才能支持XML序列化的DateTimeOffset房产吗? [英] How can I XML Serialize a DateTimeOffset Property?

查看:99
本文介绍了我怎样才能支持XML序列化的DateTimeOffset房产吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当数据被重新psented为XML $ P $的的DateTimeOffset 属性我在这个班,没有得到呈现。什么我需要做的,告诉XML序列化来呈现适合作为的DateTime 的DateTimeOffset

The DateTimeOffset property I have in this class doesn't get rendered when the data is represented as Xml. What do I need to do to tell the Xml serialization to render that proper as a DateTime or DateTimeOffset?

[XmlRoot("playersConnected")]
public class PlayersConnectedViewData
{
    [XmlElement("playerConnected")]
    public PlayersConnectedItem[] playersConnected { get; set; }
}

[XmlRoot("playersConnected")]
public class PlayersConnectedItem
{
    public string name { get; set; }
    public DateTimeOffset connectedOn { get; set; }  // <-- This property fails.
    public string server { get; set; }
    public string gameType { get; set; }
}

和一些样本数据...

and some sample data...

<?xml version="1.0" encoding="utf-8"?>
<playersConnected 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <playerConnected>
    <name>jollyroger1000</name>
    <connectedOn />
    <server>log1</server>
    <gameType>Battlefield 2</gameType>
  </playerConnected>
</playersConnected>

更新

我希望有可能是一种方法,通过一个属性,我可以对物业装修...

Update

I'm hoping there might be a way through an Attribute which I can decorate on the property...

任何办法摆脱根节点宣布这两个命名空间呢?要这样呢?

Any way to get rid of those two namespaces declared in the root node? Should I?

推荐答案

我已经结束了只是在做这...

I've ended up just doing this...

public static double ToUnixEpoch(this DateTimeOffset value)
{
    // Create Timespan by subtracting the value provided from 
    //the Unix Epoch then return the total seconds (which is a UNIX timestamp)
    return (double)((value - new DateTime(1970, 1, 1, 0, 0, 0, 0)
        .ToLocalTime())).TotalSeconds;
}

public static string ToJsonString(this DateTimeOffset value)
{
    return string.Format("\\/Date({0})\\/", value.ToUnixEpoch());
}

修改了的ViewData类...

[XmlRoot("playersConnected")]
public class PlayersConnectedItem
{
    public string name { get; set; }
    public string connectedOn { get; set; }
    public string server { get; set; }
    public string gameType { get; set; }
}

改变了我设置可视数据的属性...

var data = (from q in connectedPlayerLogEntries
            select new PlayersConnectedItem
                       {
                           name = q.ClientName,
                           connectedOn =  q.CreatedOn.ToJsonString(),
                           server = q.GameFile.UniqueName,
                           gameType = q.GameFile.GameType.Description()
                        });

完成。不知道这是最好的办法。但现在可视数据属性具有相同的值或者JSON或XML。

Done. Not sure if that's the best way .. but now that viewdata property has the same values for either Json or Xml.

这篇关于我怎样才能支持XML序列化的DateTimeOffset房产吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆