我怎样才能从序列化一个ASP.NET MVC网站XML实体? [英] How can I serialize entities from an ASP.NET MVC site to XML?

查看:80
本文介绍了我怎样才能从序列化一个ASP.NET MVC网站XML实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个ASP.NET MVC(在C#)网站,连接到一个数据库,并利用实体框架。我希望人们能够从网站修改数据库中的信息(因为它在我看来,更容易显示,并通过一个ASP.NET MVC网站验证数据不是一个桌面应用程序而被更容易​​地分发做的工具)。然后,我希望用户能够导出数据库到XML文件可以由我创建了一个桌面应用程序被加载(他们所做的更改后)。我想知道如果有一个简单的方法有由实体框架,重新present的数据库中的数据序列化的信息到一个XML文件创建的类?然后我asume我可以重复使用相同的产生code在我的桌面应用程序读取这些XML文件,并创建对象重新present的数据,在该程序中使用?

I am working on an ASP.NET MVC (in c#) site which connects to a database and takes advantage of the entities framework. I'd like people to be able to modify the information in the database from the site (since it seems to me to be much easier to display and validate data via an ASP.NET MVC site than a Desktop app while being much easier to distribute the tools to do). Then I want users to be able to export the database (after the changes they made) to an XML file which can be loaded by a desktop application I created. I'm wondering if there is an easy way to have the classes created by the entities framework which represent the data in the database serialize their information to an XML file? Then I asume I'd be able to reuse the same generated code in my desktop application to read those XML files and create the objects to represent the data for use in that program?

重要的是要只导出数据库不知何故,然后用产生的来自客户端的计算机,而不是一个XML文件(在本地数据库读取相同code中的客户端的计算机可能无法访问互联网更容易所以数据已被存储在本地)?

Would it be easier to just export the database somehow and then use the same code generated to read from a local database on the clients computer instead of an xml file (The clients computers may not be able to access the internet so the data has to be stored locally)?

您会去这个在不同?在此先感谢您的所有输入!

Would you go about this in a different? Thanks in advance for all of your input!!!

推荐答案

在XML序列化程序是你所需要的。我用我发现和我的XML响应需求略微调整了这个XmlResult类:

The Xml-serializer is all you need. I'm using this XmlResult-class that I've found and tweaked slightly for my XML-response needs:

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,你可以简单地做:

Then, whenever you want to return XML from your Action you can simply do:

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

希望帮助!

这篇关于我怎样才能从序列化一个ASP.NET MVC网站XML实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆