在Asp.NET Core MVC中反序列化XML

时间:2016-06-09 19:42:17

标签: c# asp.net-core-mvc xml-deserialization

我尝试将自定义xml反序列化程序添加到我的某个控制器的ASP.Net Core管道中。我已经能够为JSON做到这样:

    public void ConfigureServices(IServiceCollection services)
    {
        // Add framework services.
        services.AddMvc();
        services.Configure<MvcOptions>(options =>
        {
            options.RespectBrowserAcceptHeader = true;

            // Input formatters
            var xmlInputFormatting = new XmlDataContractSerializerInputFormatter();
            var jsonInputFormatting = new JsonInputFormatter();
            jsonInputFormatting.SerializerSettings.Converters.Add(new BatchContentConverter());

            options.InputFormatters.Clear();
            options.InputFormatters.Add(jsonInputFormatting);
            options.InputFormatters.Add(xmlInputFormatting);
        }
    }

(为简洁起见,已删除了许多不必要的代码)。

最终结果的控制器操作是:

    public IActionResult Post([FromBody]IBatchContent batchContent)
    {
    }

有没有办法使用XMLSerializerInputFormatter或XmlDataContractSerializerInputFormatter为XML做类似的事情?我尝试创建一个来自DataContractResolver的类,并将其分配给SerializerSettings.DataContractResolver属性,但它似乎永远不会被调用。

2 个答案:

答案 0 :(得分:1)

默认情况下,Xml格式化程序。您需要包含包含XmlSerializerInputFormatter,XmlSerializerOutputFormatter,XmlDataContractSerializerInputFormatter和XmlDataContractSerializerOutputFormatter的包Microsoft.AspNetCore.Mvc.Formatters.Xml

答案 1 :(得分:0)

您只需要执行以下操作:

  1. 创建一个继承自“Microsoft.AspNetCore.Mvc.Formatters”命名空间中的“XmlSerializerInputFormatter”类的类,并覆盖受保护的方法“CreateSerializer”。方法摘要指出“[此方法]在反序列化期间调用以获取System.Xml.Serialization.XmlSerializer”。

    public class IBatchCollectionXmlSerializer : XmlSerializerInputFormatter
    {
    
        protected override XmlSerializer CreateSerializer(Type type)
        {
            //init expected type
            Type expectedType = typeof(IBatchContent);
            //init xml serializer
            XmlSerializer serializer = null;
    
            //if not expected type
            if (expectedType != type)
            {
                //return default serializer
                serializer = base.CreateSerializer(type);
            }
            //if expected type
            else
            {
                //add concrete type to deserialize to
                Type[] extraTypes = new Type[] { typeof (BatchContentConcrete) };
                //create custom xml serializer here
                serializer = new XmlSerializer(typeof(IBatchContent), extraTypes);
            }
    
            //return serializer
            return serializer;
        }
    }
    
  2. 在Startup.cs文件中的mvc服务配置期间将其添加到输入格式化程序

    services.Configure<MvcOptions>(options =>
    {
        options.RespectBrowserAcceptHeader = true;
    
        // Input formatters
        var xmlInputFormatting = new IBatchCollectionXmlSerializer();
        var jsonInputFormatting = new JsonInputFormatter();
        jsonInputFormatting.SerializerSettings.Converters.Add(new BatchContentConverter());
    
        options.InputFormatters.Clear();
        options.InputFormatters.Add(jsonInputFormatting);
        options.InputFormatters.Add(xmlInputFormatting);
    }