XML serialization of a collection in C#

牧云@^-^@ 提交于 2019-12-07 02:48:52

问题


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:

            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]'."

How can I debug this?

Also, instead of namevaluecollection, which type of structure can i use?


回答1:


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.

Try using a container class with custom serialization:

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?

Do you intend to use it at the Book level, or higher in the object hierarchy?

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




回答2:


Look at inner exceptions.

First of all you need to make Book class serializable. Second thing is that NameValueCollection can't be serialized with XmlSerializer because:

"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)."

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



回答3:


Updated: Looks like the NamedValueCollection is your real problem as its not serializable. Perhaps you could use a Serializable dictionary.




回答4:


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. System.Collections.Specialized.NameValueCollection does not implement Add(System.String).

So the XML serializer needs a simpler kind of collection. 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.



来源:https://stackoverflow.com/questions/1465054/xml-serialization-of-a-collection-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!