我无法将ArrayList序列化为.NET中的XML。我正在使用ASP MVC并创建了一个扩展ActionResult
的类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的ArrayList
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时,我都会收到一个异常,说“不希望使用类型Assignment2.Models.Bl.Book.ReportFVO。使用XmlInclude或SoapInclude属性指定静态未知的类型。”。我没有想法。我该如何解决?提前谢谢。
答案 0 :(得分:0)
为什么使用ArrayList?自从引入仿制药以来,这已经被大大弃用了。请改用ReportFVO[]
或List<ReportFVO>
。作为替代方案,您可以尝试以下方法:
var serializer = new XmlSerializer(_data.GetType(), new Type[] { typeof(ReportFVO) });