How can I serialize entities from an ASP.NET MVC site to XML?
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?
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 a开发者_StackOverflowll of your input!!!
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);
}
}
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);
}
Hope that helps!
Try these two links to get you started xml serializer WCF documentation
Even though you have to add the WCF attributes when you may not be using WCF it can simplify the processing of the data. In my opinion it's usually better to pass this onto a trusted library where possible to prevent you being bogged down in the details
精彩评论