Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.