简体   繁体   English

C#中集合的XML序列化

[英]XML serialization of a collection in C#

I have two classes as follows: 我有两个课程如下:

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

    }

I have created an ArrayList which adds the two instances of Info class as follows: 我创建了一个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(); 

But when I try to serialize, I get an exception as "There was an error reflecting type 'System.Collections.Generic.List`1[ConsoleApplicationSerialization.Info]'." 但是当我尝试序列化时,我得到一个异常,因为“有一个错误反映了类型'System.Collections.Generic.List`1 [ConsoleApplicationSerialization.Info]'。”

How can I debug this? 我该怎么调试呢?

Also, instead of namevaluecollection, which type of structure can i use? 另外,我可以使用哪种类型的结构代替namevaluecollection?

NameValueCollection does not directly implement the ICollection interface. NameValueCollection不直接实现ICollection接口。 Instead, NameValueCollection extends NameObjectCollectionBase . 相反, NameValueCollection扩展了NameObjectCollectionBase This implements the ICollection interface, and the overloaded Add(system.string) method is not implemented in the NameValueCollection class. 这实现了ICollection接口,并且NameValueCollection类中未实现重载的Add(system.string)方法。 When you use the XMLSerializer , the XmlSerializer tries to serialize or deserialize the NameValueCollection as a generic ICollection . 当您使用XMLSerializer时XmlSerializer会尝试将NameValueCollection序列化或反序列化为通用ICollection Therefore, it looks for the default Add(System.String) . 因此,它查找默认的Add(System.String) In the absence of the Add(system.String) method, the exception is thrown. 如果没有Add(system.String)方法,则抛出异常。

Try using a container class with custom serialization: 尝试使用具有自定义序列化的容器类:

http://nayyeri.net/serialize-namevaluecollection http://nayyeri.net/serialize-namevaluecollection

However, I am unsure what you are actually trying to achieve. 但是,我不确定你实际上想要实现的目标。 What will the nvcollection contain except for the author of the book and the price, once? 除了书籍的作者和价格之外,nvcollection将包含什么?

Do you intend to use it at the Book level, or higher in the object hierarchy? 您是打算在Book级别使用它,还是在对象层次结构中使用它?

Instead of using a NameValueCollection, you might want to a Dictionary as it has more flexibility in terms of what it can contain: http://johnwsaundersiii.spaces.live.com/blog/cns!600A2BE4A82EA0A6!699.entry 您可能希望使用Dictionary而不是使用NameValueCollection,因为它在包含的内容方面具有更大的灵活性: http//johnwsaundersiii.spaces.live.com/blog/cns!600A2BE4A82EA0A6!699.entry

Look at inner exceptions. 看看内部异常。

First of all you need to make Book class serializable. 首先,您需要使Book类可序列化。 Second thing is that NameValueCollection can't be serialized with XmlSerializer because: 第二件事是NameValueCollection无法使用XmlSerializer序列化,因为:

"To be XML serializable, types which inherit from ICollection must have an implementation of Add(System.String) at all levels of their inheritance hierarchy. System.Collections.Specialized.NameValueCollection does not implement Add(System.String)." “要进行XML可序列化,从ICollection继承的类型必须在其继承层次结构的所有级别都具有Add(System.String)的实现.System.Collections.Specialized.NameValueCollection不实现Add(System.String)。”

It's message from inner exception. 这是来自内部异常的消息。

Works fine with this piece of code: 使用这段代码可以正常工作:

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

I ran your code, and the innermost exception you get is this: 我运行了你的代码,你得到的最内层的例外是:

To be XML serializable, types which inherit from ICollection must have an implementation of Add(System.String) at all levels of their inheritance hierarchy. 要进行XML可序列化,从ICollection继承的类型必须在其继承层次结构的所有级别都具有Add(System.String)的实现。 System.Collections.Specialized.NameValueCollection does not implement Add(System.String). System.Collections.Specialized.NameValueCollection未实现Add(System.String)。

So the XML serializer needs a simpler kind of collection. 因此XML序列化器需要更简单的集合。 I think your best option is to convert your NameValueCollection to a list of a custom class that contains the name and value before serialization. 我认为您最好的选择是将NameValueCollection转换为包含序列化之前的名称和值的自定义类的列表。

Updated: Looks like the NamedValueCollection is your real problem as its not serializable. 更新:看起来NamedValueCollection是您真正的问题,因为它不可序列化。 Perhaps you could use a Serializable dictionary . 也许你可以使用Serializable字典

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM