本文介绍了重写XML序列化方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有麻烦试图自定义的DateTime变量是连载于我的对象的方式。我想这是输出2011-09-26T13:00:00Z但是当我重写GetObjectData使用()函数,因为我相信是这样做的方式,没有XML数据输出它们。

  [DataContract(命名空间=)]
    [XmlRootAttribute(命名空间=htt​​p://www.w3.org/2005/Atom的ElementName =饲料)
    公共类克卡
    {
            [XmlNamespaceDeclarations]
            公共XmlSerializerNamespaces _xsns =新XmlSerializerNamespaces();

            [的XmlElement(的ElementName =条目)]
            公文集< MMU.Calendar.gCalEvent>项目=新集合< MMU.Calendar.gCalEvent>();

/ *一些其他元素* /
    }

    公共类gCalEvent
    {
            [的XmlElement(命名空间=htt​​p://schemas.google.com/g/2005)
            公共gdEvent当=新gdEvent();

/ *一些其他元素* /
    }

    公共类gdEvent:ISerializable的
    {
            [XmlAttribute(AttributeName的=startTime时)]
            私营的DateTime _startTime;
            [XmlAttribute(AttributeName的=endTime的)]
            私营的DateTime _endTime;

            公共gdEvent(日期时间的startTime,日期时间endTime的)
            {
                    _startTime = startTime时;
                    _endTime = endTime的;
            }

            公共gdEvent()
            {
                    _startTime = DateTime.MinValue;
                    _endTime = DateTime.MinValue;
            }
            [SecurityPermissionAttribute(SecurityAction.Demand,SerializationFormatter =真)
            公共虚拟无效GetObjectData使用(SerializationInfo中的信息,的StreamingContext上下文)
            {
                    //需要在格式2011-09-26T13:00:00Z
                    //如果(_startTime!= DateTime.MinValue)
                    info.AddValue(startTime时,_startTime.ToString(YYYY-MM-DDTHH:毫米:SSZ);
                    //如果(_endTime!= DateTime.MinValue)
                    info.AddValue(endTime的,_endTime.ToString(YYYY-MM-DDTHH:毫米:SSZ));
            }
    }

    克卡日历=新克卡();
    日历= readSwsS preadsheet(urlToCall);
    流=新的MemoryStream();
    XmlSerializer的序列化=新的XmlSerializer(typeof运算(兆千卡));
    serializer.Serialize(流,日历);
    stream.Seek(0,SeekOrigin.Begin);
    流的结果=新的MemoryStream();
    WebOperationContext.Current.OutgoingResponse.ContentType =为text / xml;
    返回流;
 

我也想看看这个信息了,但似乎有很多关于自定义序列化到文件,而不是XML ...

解决方案

我已经从@craighawker建议调用set()方法的日期时间时,日期时间格式化成字符串

。似乎是最简单的方法,但我想实现它使用的IXmlSerializable在一些点在未来做更多的强大的东西的正确方法

 公共类gdEvent
    {
    [XmlAttribute(AttributeName的=startTime时)]
    私人字符串m_startTimeOutput;
    私营的DateTime m_startTime; //格式2011-11-02T09:00:00Z

    [XmlAttribute(AttributeName的=endTime的)]
    私人字符串m_endTimeOutput;
    私营的DateTime m_endTime; // 2011-11-02T10:00:00Z

    公开日期时间的startTime
    {
        得到
        {
        返回m_startTime;
        }
        组
        {
        m_startTime =价值;
        m_startTimeOutput = m_startTime.ToString(YYYY-MM-DDTHH:毫米:SSZ);
        }
    }

    公开日期时间endTime的
    {
        得到
        {
        返回m_endTime;
        }
        组
        {
        m_endTime =价值;
        m_endTimeOutput = m_endTime.ToString(YYYY-MM-DDTHH:毫米:SSZ);
        }
    }
 

I'm having trouble trying to customize the way DateTime variables are serialized in my objects. I want it to be output as 2011-09-26T13:00:00Z but when I override the GetObjectData() function as I believe is the way to do this, no XML data is output for them at all.

    [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;

I have tried to look this info up but there seems to be a lot about custom serializing to files but not to XML...

解决方案

I had a suggestion from @craighawker to format the DateTime into a string when calling a set() method for the DateTime. Seemed the simplest method although I would like to implement it the proper way using IXMLSerializable at some point in the future to do more powerful things

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

这篇关于重写XML序列化方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 03:45