如何在ASP.NET MVC4中将数据库表转换为XML

如何在ASP.NET MVC4中将数据库表转换为XML,asp.net,xml,asp.net-mvc,xml-serialization,xmlserializer,Asp.net,Xml,Asp.net Mvc,Xml Serialization,Xmlserializer,我正在尝试将SQLServer数据库表转换为XML文件。我跟着。 我已经创建了这个类,如解决方案中所示 public class XmlResult : ActionResult { private object objectToSerialize; /// <summary> /// Initializes a new instance of the <see cref="XmlResult"/> class. //

我正在尝试将SQLServer数据库表转换为XML文件。我跟着。 我已经创建了这个类,如解决方案中所示

public class XmlResult : ActionResult
{
    private object objectToSerialize;

    /// <summary>
    /// Initializes a new instance of the <see cref="XmlResult"/> class.
    /// </summary>
    /// <param name="objectToSerialize">The object to serialize to XML.</param>
    public XmlResult(object objectToSerialize)
    {
        this.objectToSerialize = objectToSerialize;
    }

    /// <summary>
    /// Gets the object to be serialized to XML.
    /// </summary>
    public object ObjectToSerialize
    {
        get { return this.objectToSerialize; }
    }

    /// <summary>
    /// Serialises the object that was passed into the constructor to XML and writes the corresponding XML to the result stream.
    /// </summary>
    /// <param name="context">The controller context for the current request.</param>
    public override void ExecuteResult(ControllerContext context)
    {
        if (this.objectToSerialize != null)
        {
            context.HttpContext.Response.Clear();
            XmlRootAttribute root = new XmlRootAttribute("response");

            var xs = new System.Xml.Serialization.XmlSerializer(this.objectToSerialize.GetType(), root);
            context.HttpContext.Response.ContentType = "text/xml";

            xs.Serialize(context.HttpContext.Response.Output, this.objectToSerialize);
        }
    }
我写了以下内容(我的目的是获得所有产品):

在调试过程中,出现以下错误:

要使XML可序列化,从IEnumerable继承的类型必须具有 Add(System.Object)在其所有级别的实现 继承层次结构。 System.Data.Entity.Infrastructure.DbQuery`1[[Overstock.Models.Product, 库存过剩,版本=1.0.0.0,区域性=中立,PublicKeyToken=null]] 未实现添加(System.Object)

源错误:

第42行:var xs=new System.Xml.Serialization.XmlSerializer(this.objectToSerialize.GetType(), 根)

我假设出现错误是因为我以错误的方式获取产品:

var product=数据库中的s。产品选择s


为了将SQL Server表转换为XML文件格式,我应该以何种形式将数据发送到XmlResult类?

如果此方法中的“db”是DbContext

var product = from s in db.Product
                  select s;
// fetch stuff in database
return new XmlResult(product);
然后,您不会得到一个DataRow或DataTable,而是得到一个强类型类的集合。如果要从它们生成xml,请使用以下代码:

public static string SerializeAsXml<T>(T element)
{
    XmlSerializer xmlSerializer = new XmlSerializer(element.);
    StringWriter textWriter = new StringWriter();

    xmlSerializer.Serialize(textWriter, element.GetType());
    return textWriter.ToString();
}
var product = from s in db.Product
                  select s;
// fetch stuff in database
return new XmlResult(product);
public static string SerializeAsXml<T>(T element)
{
    XmlSerializer xmlSerializer = new XmlSerializer(element.);
    StringWriter textWriter = new StringWriter();

    xmlSerializer.Serialize(textWriter, element.GetType());
    return textWriter.ToString();
}
var products = from s in db.Product
                  select s;
// fetch stuff in database
return SerializeAsXml(products);