使用XmlRootAttribute类时是否需要刷新?

时间:2013-03-20 09:15:58

标签: c# serialization xml-serialization flush xmlroot

我知道在使用流时我需要刷新它以便释放与流相关的任何资源,并且刷新流会获取尚未写入的任何缓冲数据,并立即将其写出来。

我有以下课程:

[XmlRoot("EventList")]
public class EventList
{
    private List<BaseEvent> events_ = new List<BaseEvent>();
    /// Default constructor. Required for serialization
    public EventList() {}

    [XmlElement(ElementName = "Event")]
    public List<BaseEvent> Events
    {
        get 
        {
            return events_;
        }
        set 
        {
            events_ = value;
        }
    }
}

现在在我发送它作为对HTTPRequest的响应之前,我是否需要刷新它?如果是这样我怎么办?

我问,因为我有这里描述的问题: AJAX response - XmlHttp.responseXML is cut off

js客户端激活的方法是:

[WebMethod]
public EventsRequestResult GetEvents(string agentId, int delayedReturnMS)

// EventsRequestResult - return value of the GetEvents method of the EventsService.
public class EventsRequestResult
{
    private EventList events_ = null;
    /// <summary>
    /// List of the events
    /// </summary>
    [XmlElement(ElementName = "Events")]
    public EventList Events
    {
        get
        {
            return events_;
        }
        set
        {
            events_ = value;
        }
    }
    /// Default constructor.
    public EventsRequestResult(){}
    /// <summary>
    /// Constructor used in case of the failure.
    /// </summary>
    /// <param name="errorCode">Numeric error code</param>
    /// <param name="errorDescription">Human readable error description</param>
    public EventsRequestResult(RequestErrorCode errorCode, string errorDescription) : base(errorCode, errorDescription){}
    /// <summary>
    /// Constructor used in case of successful events request.
    /// </summary>
    /// <param name="events">List of the events</param>
    public EventsRequestResult(EventList events) : base()
    {
        events_ = events;
    }
}

1 个答案:

答案 0 :(得分:0)

Marc在上面的评论中是正确的,但我认为你正在寻找的是像

    using(var stream = theResponse.OutputStream)
    {
        var ser = new XmlSerializer(typeof(EventList));
        return ser.Deserialize(stream) as EventList;
    }
使用块中的

return将关闭(并刷新)流。