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

时间:2014-03-14 19:35:46

标签: asp.net xml asp.net-mvc xml-serialization xmlserializer

我正在尝试将SQL Server数据库表转换为XML文件。我跟着this solution。 我创建了这个类,如解决方案

所示
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);
        }
    }

而不是:

public ActionResult GetStuffAsXml(int id)
{
    var dbStuff = db.GetStuff(id);
    // fetch stuff in database
    return new XmlResult(dbStuff);
}

我写过这个(我的目的是获得所有产品):

public ActionResult Transfer()
        {
            var product = from s in db.Product
                      select s;
            // fetch stuff in database
            return new XmlResult(product);
        }

在调试过程中,出现了这个错误:

  

要成为XML可序列化,从IEnumerable继承的类型必须具有   Add(System.Object)在其所有级别的实现   继承层次结构。   System.Data.Entity.Infrastructure.DbQuery`1 [Overstock.Models.Product,   Overstock,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null]]   没有实现Add(System.Object)。

来源错误:

  

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

我认为错误即将发生,因为我正在以错误的方式处理产品:

  

var product = from db.Product中的s选择s;

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

1 个答案:

答案 0 :(得分:1)

如果'db'是此方法中的DbContext

var product = from s in db.Product
                  select s;
// fetch stuff in database
return new XmlResult(product);

然后你没有得到DataRow或DataTable,你得到了一个强类型类的集合。如果你想要make 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 products = from s in db.Product
                  select s;
// fetch stuff in database
return SerializeAsXml(products);