在C#中反序列化自定义XML数据类型

时间:2011-01-17 22:12:36

标签: c# xml serialization types

我有一个我无法控制的xml文档,它有一个带有自定义数据类型的元素

<foo>
   <time type="epoch_seconds">1295027809.26896</time>
</foo>

我想有一个可以自动转换为Epoch秒的类:

[Serializable]
public class Foo
{
      public Foo()
      {
      }

      public EpochTime Time { get; set; }
}

有没有办法定义EpochTime类,以便XML序列化程序知道在使用type="epoch_time"查找XML时使用它?如果是这样,我如何设置WriteXmlReadXml来执行此操作?

2 个答案:

答案 0 :(得分:4)

通常的方法是使用一个符合您预期的属性来简单地填充它:

public class EpochTime {
    public enum TimeType {
       [XmlEnum("epoch_seconds")] Seconds
    }
    [XmlAttribute("type")] public TimeType Type {get;set;}
    [XmlText] public string Text {get;set;}

    [XmlIgnore] public DateTime Value {
        get { /* your parse here */ }
        set { /* your format here */ }
    }
}

另外,你需要:

[XmlElement("time")]
public EpochTime Time { get; set; }

以下是xml的完整示例:

using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
static class Program
{
    static void Main()
    {
        Foo foo;
        var ser = new XmlSerializer(typeof(Foo));
        using (var reader = XmlReader.Create(new StringReader(@"<foo>
   <time type=""epoch_seconds"">1295027809.26896</time>
</foo>")))
        {
            foo = (Foo)ser.Deserialize(reader);
        }
    }
}
public class EpochTime
{
    public enum TimeType
    {
        [XmlEnum("epoch_seconds")]
        Seconds
    }
    [XmlAttribute("type")]
    public TimeType Type { get; set; }
    [XmlText]
    public string Text { get; set; }
    private static readonly DateTime Epoch = new DateTime(1970, 1, 1);
    [XmlIgnore] public DateTime Value
    {
        get
        {
            switch (Type)
            {
                case TimeType.Seconds:
                    return Epoch + TimeSpan.FromSeconds(double.Parse(Text));
                default:
                    throw new NotSupportedException();
            }
        }
        set {
            switch (Type)
            {
                case TimeType.Seconds:
                    Text = (value - Epoch).TotalSeconds.ToString();
                    break;
                default:
                    throw new NotSupportedException();
            }
        }
    }
}
[XmlRoot("foo")]
public class Foo
{
    public Foo()
    {
    }

    [XmlElement("time")]
    public EpochTime Time { get; set; }
}

答案 1 :(得分:0)

您真的需要实施ISerializable吗?以下内容可能适用于您的方案:

public class EpochTime
{
    [XmlText]
    public double Data { get; set; }
    [XmlAttribute("type")]
    public string Type { get; set; }
}

public class Foo
{
    public EpochTime Time { get; set; }
}

class Program
{
    public static void Main()
    {
        var foo = new Foo
        {
            Time = new EpochTime
            {
                Data = 1295027809.26896,
                Type = "epoch_seconds"
            }
        };
        var serializer = new XmlSerializer(foo.GetType());
        serializer.Serialize(Console.Out, foo);
    }
}

另请注意,[Serializable]XmlSerializer没有影响。