J'essaie de sérialiser ModelStateDictionary en une chaîne XML. J'ai essayé avec la classe de sérialisation XML .net comme ceci:Sérialiser ModelStateDictionary au format XML
XmlSerializer serializer = new XmlSerializer(typeof(ModelStateDictionary));
TextWriter textWriter = new StreamWriter("c:\\files\\text.xml");
serializer.Serialize(textWriter,this.ModelState);
textWriter.Close();
Le résultat a été:
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfKeyValuePairOfStringModelState xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<KeyValuePairOfStringModelState />
<KeyValuePairOfStringModelState />
<KeyValuePairOfStringModelState />
<KeyValuePairOfStringModelState />
<KeyValuePairOfStringModelState />
<KeyValuePairOfStringModelState />
<KeyValuePairOfStringModelState />
<KeyValuePairOfStringModelState />
<KeyValuePairOfStringModelState />
<KeyValuePairOfStringModelState />
<KeyValuePairOfStringModelState />
<KeyValuePairOfStringModelState />
<KeyValuePairOfStringModelState />
<KeyValuePairOfStringModelState />
<KeyValuePairOfStringModelState />
</ArrayOfKeyValuePairOfStringModelState>
Donc, pour l'essayer d'une autre manière je SharpSerializer avec ce code:
SharpSerializer serializer = new SharpSerializer();
serializer.Serialize(ModelState, "c:\\files\\text.xml");
Cela m'a donné une erreur:
Unable to cast object of type 'System.Web.Mvc.ModelStateDictionary' to type 'System.Collections.ICollection'.
Avec ce stacktrace:
[InvalidCastException: Unable to cast object of type 'System.Web.Mvc.ModelStateDictionary' to type 'System.Collections.ICollection'.]
Polenter.Serialization.Serializing.PropertyFactory.parseCollectionItems(CollectionProperty property, TypeInfo info, Object value) +121
Polenter.Serialization.Serializing.PropertyFactory.fillCollectionProperty(CollectionProperty property, TypeInfo info, Object value) +75
Polenter.Serialization.Serializing.PropertyFactory.CreateProperty(String name, Object value) +679
Polenter.Serialization.SharpSerializer.Serialize(Object data, Stream stream) +196
Polenter.Serialization.SharpSerializer.Serialize(Object data, String filename) +111
Getronics.Web.Portal.Controllers.BaseCiController`1.Create(String btnaction, TRecordType dto) in C:\projects\Getronics\Getronics.Web.Portal\Controllers\BaseCiController.cs:58
lambda_method(Closure , ControllerBase , Object[]) +228
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +208
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +27
System.Web.Mvc.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12() +55
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +263
System.Web.Mvc.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() +19
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +263
System.Web.Mvc.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() +19
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +191
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +343
System.Web.Mvc.Controller.ExecuteCore() +116
System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +97
System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10
System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +37
System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21
System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +12
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +50
System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8963149
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184
Donc pas de chance ici. Bien que je m'entende vraiment sérialiser le modèle. Est-ce que quelqu'un sait comment faire fonctionner SharpSerializer avec un objet Dictionary? Ou peut-être un sérialiseur différent?
pourquoi, au lieu de typeof (ModelState) – tkt986
avez-vous typeof (ModelStateDictionary) parce que le ModelState est un ModelStateDictionary .. – Patrick