覆盖XML序列化方法

ank*_*les 5 .net c# xml

我在尝试自定义DateTime变量在对象中序列化的方式时遇到了麻烦.我希望它输出为2011-09-26T13:00:00Z但是当我覆盖GetObjectData()函数时,我认为是这样做的方法,根本不输出任何XML数据.

    [DataContract(Namespace = "")]
    [XmlRootAttribute(Namespace = "http://www.w3.org/2005/Atom", ElementName = "feed")]
    public class GCal
    {
            [XmlNamespaceDeclarations]
            public XmlSerializerNamespaces _xsns = new XmlSerializerNamespaces();

            [XmlElement(ElementName = "entry")]
            public Collection<MMU.Calendar.gCalEvent> items = new Collection<MMU.Calendar.gCalEvent>();

/*some other elements*/
    }

    public class gCalEvent
    {
            [XmlElement(Namespace = "http://schemas.google.com/g/2005")]
            public gdEvent when = new gdEvent();

/*some other elements*/
    }

    public class gdEvent : ISerializable
    {
            [XmlAttribute(AttributeName = "startTime")]
            private DateTime _startTime; 
            [XmlAttribute(AttributeName = "endTime")]
            private DateTime _endTime;

            public gdEvent(DateTime startTime, DateTime endTime)
            {
                    _startTime = startTime;
                    _endTime = endTime;
            }

            public gdEvent()
            {
                    _startTime = DateTime.MinValue;
                    _endTime = DateTime.MinValue;
            }
            [SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
            public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
            {
                    //needs to be in the format 2011-09-26T13:00:00Z
                    //if (_startTime != DateTime.MinValue)
                    info.AddValue("startTime", _startTime.ToString("yyyy-MM-ddTHH:mm:ssZ");
                    //if (_endTime != DateTime.MinValue)
                    info.AddValue("endTime", _endTime.ToString("yyyy-MM-ddTHH:mm:ssZ"));
            }
    }

    GCal calendar = new GCal();
    calendar = readSwsSpreadsheet(urlToCall);
    stream = new MemoryStream();
    XmlSerializer serializer = new XmlSerializer(typeof(GCal));
    serializer.Serialize(stream, calendar);
    stream.Seek(0, SeekOrigin.Begin);
    Stream results = new MemoryStream();
    WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
    return stream;
Run Code Online (Sandbox Code Playgroud)

我试图查看这些信息,但似乎有很多关于自定义序列化到文件而不是XML ...

luk*_*san 8

您正在尝试为包含类型使用XmlSerializer序列化的属性类型自定义标准序列化(ISerializable).您需要实现IXmlSerializable接口来自定义XML序列化.尝试这样的事情:

public class gdEvent : IXmlSerializable
{
    private DateTime _startTime;
    private DateTime _endTime;

    public gdEvent(DateTime startTime, DateTime endTime)
    {
        _startTime = startTime;
        _endTime = endTime;
    }

    public gdEvent()
    {
        _startTime = DateTime.MinValue;
        _endTime = DateTime.MinValue;
    }

    public XmlSchema GetSchema()
    {
        return null;
    }

    public void WriteXml(XmlWriter writer)
    {
        if (_startTime != DateTime.MinValue)
            writer.WriteAttributeString("startTime", _startTime.ToString("yyyy-MM-ddTHH:mm:ssZ"));
        if (_endTime != DateTime.MinValue)
            writer.WriteAttributeString("endTime", _endTime.ToString("yyyy-MM-ddTHH:mm:ssZ"));
    }

    public void ReadXml(XmlReader reader)
    {
        string startTimeString = reader.GetAttribute("startTime");
        if (!string.IsNullOrEmpty(startTimeString))
        {
            _startTime = DateTime.Parse(startTimeString);
        }
        string endTimeString = reader.GetAttribute("startTime");
        if (!string.IsNullOrEmpty(endTimeString))
        {
            _endTime = DateTime.Parse(endTimeString);
        }
    }
Run Code Online (Sandbox Code Playgroud)

}


ank*_*les 2

@craighawker 建议我在调用 DateTime 的 set() 方法时将 DateTime 格式化为字符串。似乎是最简单的方法,尽管我想在将来的某个时候使用 IXMLSerialized 以正确的方式实现它,以完成更强大的事情

 public class gdEvent
    {
    [XmlAttribute(AttributeName = "startTime")]
    private string m_startTimeOutput;
    private DateTime m_startTime; //format 2011-11-02T09:00:00Z

    [XmlAttribute(AttributeName = "endTime")]
    private string m_endTimeOutput;
    private DateTime m_endTime; //2011-11-02T10:00:00Z

    public DateTime startTime
    {
        get
        {
        return m_startTime;
        }
        set
        {
        m_startTime = value;
        m_startTimeOutput = m_startTime.ToString("yyyy-MM-ddTHH:mm:ssZ");
        }
    }

    public DateTime endTime
    {
        get
        {
        return m_endTime;
        }
        set
        {
        m_endTime = value;
        m_endTimeOutput = m_endTime.ToString("yyyy-MM-ddTHH:mm:ssZ");
        }
    }
Run Code Online (Sandbox Code Playgroud)