[英]Override XML Serialization Method
我在嘗試自定義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;
我試圖查看這些信息,但似乎有很多關於自定義序列化到文件而不是XML ...
您正在嘗試為包含類型使用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);
}
}
}
我有一個來自@craighawker的建議,在為DateTime調用set()方法時將DateTime格式化為字符串。 看似最簡單的方法,雖然我想在未來的某個時候使用IXMLSerializable以正確的方式實現它來做更強大的事情
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");
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.