序列化到 XML ArrayList<T>;在 .NET Core 3 中 [英] Serializing to XML ArrayList&lt;T&gt; in .NET Core 3

查看:29
本文介绍了序列化到 XML ArrayList<T>;在 .NET Core 3 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法从 .net core 3 中的控制器序列化 ActionResult,其中 ArrayList 由某种类型 T 组成>(在本例中为 Person).

它不会序列化为 XML,只会序列化为 JSON.它抱怨 Person 类型未知(即使它是,并且它本身或作为数组序列化得很好).

即这将失败序列化:

[HttpGet("List")]公共 ActionResult所有人() {...}

这有效:

[HttpGet("List")]公共 ActionResult所有人() {...}

所以 Person 类型 (T) 本身可以很好地序列化,并且 Person[] 也可以很好地序列化,但是当ArrayList(Person)的 XML 序列化失败:

<块引用>

System.InvalidOperationException: 产生错误XML 文档.---> System.InvalidOperationException: 类型SimpleRESTServer.Models.Person 不是预期的.使用 XmlInclude 或SoapInclude 属性用于指定静态未知的类型.

(Person 类型是静态已知的,PersonPerson[] 都可以序列化):

[XmlInclude(typeof(Person))][可序列化]公开课人...

解决方案

ArrayList已弃用一段时间.正如@MindingData 在评论中提到的ArrayList 不是通用的.

您应该考虑使用 List 而不是 ArrayList.我大概有十年没有看到 ArrayList 使用了——有可能大多数序列化框架不会优雅地处理它,至少部分是因为它既没有实现 IEnumerable;ICollection.迁移到更新的泛型集合类型可能会解决您在处理旧的、不推荐使用的非泛型集合类型时遇到的任何奇怪错误.

您可以通过在测试用例中使用 object[] 而不是 Person[] 来测试这个理论.ArrayList 更接近于 object[],而 List 更接近于 Person[].(也就是说,一些序列化框架即使在无法处理 ArrayList 时也能正确处理 object[],所以这不是一个完美的测试.)>

Is there a way to serialize ActionResult<ArrayList> from a controller in .net core 3, where ArrayList is composed of some type T (Person in this case).

It won't serialize to XML, only JSON. It complains the type Person is not known (even though it is, and it serializes just fine itself or as an array).

i.e. this fails serialization:

[HttpGet("List")]
public ActionResult<ArrayList> AllPersons() {...}

this works:

[HttpGet("List")]
public ActionResult<Person[]> AllPersons() {...}

So the Person type (T) can serialize just fine by itself, and Person[] also serializes just fine, but when an ArrayList (of Person) fails XML serialization with:

System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: The type SimpleRESTServer.Models.Person was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.

(Person type is known statically, and both Person and Person[] will serialize just fine):

[XmlInclude(typeof(Person))]
[Serializable]
public class Person ...

解决方案

ArrayList has been deprecated for quite a while. As @MindingData mentioned in a comment, ArrayList isn't generic.

You should look into using List<Person> instead of ArrayList. I haven't seen ArrayList used in probably about a decade--chances are most serialization frameworks aren't going to handle it gracefully, at least in part because it implements neither IEnumerable<T> nor ICollection<T>. Migrating to newer, generic collection types will probably resolve any strange errors you encounter while dealing with older, deprecated, non-generic collection types.

You may be able to test this theory by using object[] instead of Person[] in your test case. ArrayList is closer to object[], whereas List<Person> is closer to Person[]. (That being said, some serialization frameworks will handle object[] correctly even when they're unable to handle ArrayList, so this isn't a perfect test.)

这篇关于序列化到 XML ArrayList<T>;在 .NET Core 3 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆