2011-01-17 12 views
5

मैं एक xml दस्तावेज है कि मुझे लगता है कि एक कस्टम डेटाप्रकार साथ एक तत्व है को नियंत्रित नहीं करते हैdeserialize सी # में कस्टम एक्सएमएल डेटाप्रकार

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

मैं एक वर्ग कि स्वचालित रूप से युग सेकंड के लिए परिवर्तित कर सकते हैं करना चाहते हैं:

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

     public EpochTime Time { get; set; } 
} 

वहाँ इतना है कि एक्सएमएल serializer जब type="epoch_time" साथ एक्सएमएल खोजने के लिए इसका इस्तेमाल करने के लिए जानता है एक EpochTime वर्ग को परिभाषित करने के लिए एक तरीका है? और यदि हां, तो मैं इसे करने के लिए WriteXml और ReadXml कैसे स्थापित करूं?

+0

('[Serializable]' नहीं एक्सएमएल क्रमबद्धता को प्रभावित करें) –

उत्तर

4

सामान्य तरीके से बस एक संपत्ति बर्ताव करता है कि आपकी अपेक्षा के साथ यह शिम है

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

यहाँ के साथ एक पूर्ण उदाहरण है आपके एक्सएमएल:

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; } 
} 
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 पर कोई प्रभाव नहीं पड़ता है।

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

भी, आप की आवश्यकता होगी:

संबंधित मुद्दे