小编utk*_*den的帖子

与 XmlAttributeOverrides 一起使用时,序列化 List<T> 的 XmlSerializer 的构造函数会引发 InvalidOperationException

概括

使用XmlSerializer该类时,使用以下方法序列化 a List<T>(其中 T 可以XmlSerializer毫无问题地序列化)XmlAttributeOverrides

using xmls = System.Xml.Serialization;
...
            xmls.XmlAttributeOverrides attributeOverrides = new xmls.XmlAttributeOverrides();
            attributeOverrides.Add(typeof(T), new xmls.XmlAttributes()
            {
                XmlRoot = new xmls.XmlRootAttribute("foo")
            });
            attributeOverrides.Add(typeof(List<T>), new xmls.XmlAttributes()
            {                    
                XmlArray = new xmls.XmlArrayAttribute("foobar"),
                XmlArrayItems = { new xmls.XmlArrayItemAttribute("foo") },                    
            });
Run Code Online (Sandbox Code Playgroud)

将在最内部的异常处抛出以下 InvalidOperationExcpetion:

System.InvalidOperationException: XmlRoot and XmlType attributes may not be specified for the type System.Collections.Generic.List`1[[T, programname, Version=versionnumber, Culture=neutral, PublicKeyToken=null]].
Run Code Online (Sandbox Code Playgroud)

我对序列化程序的期望

<texparams>
    <texparam pname="TextureMinFilter" value="9729"/>
    <texparam pname="TextureMagFilter" value="9729"/>
</texparams>
Run Code Online (Sandbox Code Playgroud)

我现在能成功得到什么

<ArrayOfTextureParameter xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <TextureParameter …
Run Code Online (Sandbox Code Playgroud)

c# xml serialization list

3
推荐指数
1
解决办法
265
查看次数

标签 统计

c# ×1

list ×1

serialization ×1

xml ×1