C# C语言中集合的XML序列化#

C# C语言中集合的XML序列化#,c#,xml-serialization,namevaluecollection,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)

我有两门课,如下:

    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());
            }

    }
            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(); 
我创建了一个ArrayList,其中添加了Info类的两个实例,如下所示:

    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());
            }

    }
            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(); 
FileStream fs=newfilestream(“SerializedInfo.XML”,FileMode.Create);
List arrList=新列表();
XmlSerializer xs=新的XmlSerializer(typeof(List));
Info pObj=新信息(“ABC”,3,“DEF”,2,6);
Info pObj1=新信息(“GHI”,4,“JKL”,2,8);
arrList.Add(pObj);
arrList.Add(pObj1);
序列化(fs,arrList);
fs.Close();
但是,当我尝试序列化时,会出现一个异常,原因是“反映类型'System.Collections.Generic.List`1[ConsoleApplicationSerialization.Info]'的错误。”

我如何调试这个


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

我运行了您的代码,您得到的最内部的异常是:

要实现XML序列化,请键入 从ICollection继承必须具有 Add(System.String)的实现 在他们继承的各个层次上 等级制度 System.Collections.Specialized.NameValueCollection 未实现添加(System.String)


因此,XML序列化程序需要一种更简单的集合。我认为您最好的选择是将您的
NameValueCollection
转换为一个自定义类的列表,其中包含序列化之前的名称和值。

更新:看来NameValueCollection是您真正的问题,因为它不可序列化。也许您可以使用一个。

查看内部异常

首先,您需要使Book类可序列化。 第二件事是无法使用XmlSerializer序列化NameValueCollection,因为:

要使XML可序列化,请键入 从ICollection继承必须具有 Add(System.String)的实现 在他们继承的各个层次上 等级制度 System.Collections.Specialized.NameValueCollection 不执行 添加(System.String)。”

这是来自内部异常的消息

这段代码可以正常工作:

  [Serializable]
        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());
            }
        }
NameValueCollection不直接 实现ICollection接口。 相反,NameValueCollection扩展了 NameObjectCollectionBase。这 实现ICollection接口, 以及重载的Add(system.string) 方法未在中实现 NameValueCollection类。当你 使用XMLSerializer,将 XmlSerializer尝试序列化或 将名称值集合反序列化为 一个通用的ICollection。因此, 查找默认值 添加(System.String)。在没有 Add(system.String)方法 异常被抛出

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

然而,我不确定你到底想要实现什么。除了这本书的作者和价格之外,这本书的藏品将包含哪些内容

您打算在书本级别或对象层次结构的更高级别使用它吗


与使用NameValueCollection不同,您可能希望使用字典,因为它在包含内容方面具有更大的灵活性:

与使用标准序列化处理的类不同,类不必具有可序列化属性才能使用XML序列化处理。我的错误。看起来您需要使用一些自定义序列化代码。如果我只从Book类中删除namevaluecollection,则一切正常。
[Serializable]
属性对XML序列化并不重要。