Skip to content

Instantly share code, notes, and snippets.

@mdellanoce
Created December 15, 2011 00:57
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save mdellanoce/1479347 to your computer and use it in GitHub Desktop.
ASP.NET MVC (Version 1) XML Model Binder
public class BindXmlAttribute : CustomModelBinderAttribute
{
public override IModelBinder GetBinder()
{
return new XmlModelBinder();
}
}
public ActionResult Edit(
[ModelBinder(typeof(XmlModelBinder))] string xml)
{
//...
}
public ActionResult Edit(
[ModelBinder(typeof(XmlModelBinder))] MyXmlSerializableClass xml)
{
//...
}
public ActionResult Edit(
[BindXml] MyXmlSerializableClass xml)
{
//...
}
public class XmlModelBinder : IModelBinder
{
object IModelBinder.BindModel(
ControllerContext controllerContext,
ModelBindingContext bindingContext)
{
var stream = controllerContext.HttpContext.Request.InputStream;
using (var reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
}
public class XmlModelBinder : IModelBinder
{
object IModelBinder.BindModel(
ControllerContext controllerContext,
ModelBindingContext bindingContext)
{
var stream = controllerContext.HttpContext.Request.InputStream;
var serializer = new XmlSerializer(bindingContext.ModelType);
return serializer.Deserialize(stream);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment