2011-04-30 30 views
0

我在將.NET中的ArrayList序列化爲XML時遇到了問題。我使用ASP MVC和創建延伸的ActionResult如何序列化爲XML ArrayList

public class XmlResult : ActionResult 
{ 
    private readonly object _data; 

    public XmlResult(object data) 
    { 
     _data = data; 
    } 

    public override void ExecuteResult(ControllerContext context) 
    { 
     if (_data != null) 
     { 
      var response = context.HttpContext.Response; 
      response.ContentType = "text/xml"; 
      var serializer = new XmlSerializer(_data.GetType(), "Assignment2.Models.Bl.Book"); 

      serializer.Serialize(response.OutputStream, _data); 
     } 
    } 
} 

類現在_data是ReportFVO

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Xml.Serialization; 

namespace Assignment2.Models.Bl.Book 
{ 
    [XmlInclude(typeof(ReportFVO))] 
    public class ReportFVO 
    { 
     public String title { get; set; } 
     public String author { get; set; } 
     public String first_name { get; set; }   
     public String last_name { get; set; } 

     public ReportFVO() 
     { 
      this.title = ""; 
      this.author = ""; 
      this.first_name = ""; 
      this.last_name = ""; 
     } 
    } 
} 

每次XmlResult的ArrayList被稱爲我得到一個異常說「之類Assignment2.Models。不要求Bl.Book.ReportFVO。使用XmlInclude或SoapInclude屬性指定靜態未知的類型。「我沒有想法。我該如何解決?提前致謝。

回答

0

爲什麼使用ArrayList?自引入泛型以來,這已經過時了。改爲使用ReportFVO[]List<ReportFVO>。作爲替代,您可以嘗試以下操作:

var serializer = new XmlSerializer(_data.GetType(), new Type[] { typeof(ReportFVO) });