c# - C# 中集合的 XML 序列化

标签 c# xml-serialization namevaluecollection

我有两个类如下:

    public class Info
    { 
        [XmlAttribute] public string language;
        public int version;
        public Book book;

        public Info() { }

        public Info(string l, int v, string author, int quantity, int price)
        {
        this.language = l;
        this.version = v;
        book = new Book(author, quantity, price);
        }


    }

    public class Book
    {
            [XmlAttribute] public string author;
            public int quantity;
            public int price;
            [XmlIgnore]public int total;
            public NameValueCollection nvcollection = new NameValueCollection();

            public Book() { }

            public Book(string author, int quantity, int price)
            {
            this.author = author;
            this.quantity = quantity;
            this.price = price;
            total = quantity * price;
            nvcollection.Add(author, price.ToString());
            }

    }

我创建了一个 ArrayList,它添加了 Info 类的两个实例,如下所示:

            FileStream fs = new FileStream("SerializedInfo.XML", FileMode.Create);

            List<Info> arrList = new List<Info>();

            XmlSerializer xs = new XmlSerializer(typeof(List<Info>));

            Info pObj = new Info("ABC", 3, "DEF", 2, 6);

            Info pObj1 = new Info("GHI", 4, "JKL", 2, 8);

            arrList.Add(pObj);
            arrList.Add(pObj1);

            xs.Serialize(fs, arrList);

            fs.Close(); 

但是当我尝试序列化时,出现异常“反射(reflect)类型 'System.Collections.Generic.List`1[ConsoleApplicationSerialization.Info]' 时出现错误。”

我该如何调试?

此外,我可以使用哪种类型的结构来代替 namevaluecollection?

最佳答案

NameValueCollection does not directly implement the ICollection interface. Instead, NameValueCollection extends NameObjectCollectionBase. This implements the ICollection interface, and the overloaded Add(system.string) method is not implemented in the NameValueCollection class. When you use the XMLSerializer, the XmlSerializer tries to serialize or deserialize the NameValueCollection as a generic ICollection. Therefore, it looks for the default Add(System.String). In the absence of the Add(system.String) method, the exception is thrown.

尝试使用具有自定义序列化的容器类:

http://nayyeri.net/serialize-namevaluecollection

但是,我不确定您实际想要实现的目标是什么。一次 nvcollection 除了书的作者和价格,还会包含什么?

您打算在 Book 级别还是在对象层次结构的更高级别使用它?

您可能不使用 NameValueCollection,而是使用字典,因为它在包含的内容方面具有更大的灵 active :http://johnwsaundersiii.spaces.live.com/blog/cns!600A2BE4A82EA0A6!699.entry

关于c# - C# 中集合的 XML 序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1465054/

相关文章:

c# - 更新 net5.0 .csproj 到 net6.0

asp.net - NameValueCollection 到 URL 查询?

时间:2019-03-17 标签:c#NameValueCollectiondistinctvalues

c# - Entity Framework 批量插入虚幻缓慢

c# - EntityFramework - 将复杂属性映射到 json(字符串)列

c# - 允许 .NET WebApi 忽略 DOCTYPE 声明

c# - 如何在 C# 中序列化/反序列化可选的 XML 枚举?

.net - NameValueCollection 的高效搜索

c# - 在 View 中显示连接 linq 查询

c# - .NET Xml 序列化 : Integer Element with Attribute?