开发者

Deserialize custom XML datatype in C#

开发者 https://www.devze.com 2023-02-05 17:51 出处:网络
I have an xml document that I don\'t control that has an element with a custom datatype <foo> <time type=\"epoch_seconds\">1295027809.26896</time>

I have an xml document that I don't control that has an element with a custom datatype

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

I would like to have a class that could automatically convert to Epoch seconds:

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

      public EpochTime Time { get; set; }
}

Is there a way开发者_JAVA百科 to define an EpochTime class so that the XML serializer knows to use it when finding XML with type="epoch_time"? And if so, how do I set up the WriteXml and ReadXml to do it?


The normal way is to simply shim it with a property that behaves as you expect:

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 */ }
    }
}

also, you would need:

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

Here's a complete example with your 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; }
}


Do you really need to implement ISerializable? The following might work in your scenario:

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

Also notice that [Serializable] has no effect on XmlSerializer.

0

精彩评论

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