在C#中的集合XML序列化 [英] XML serialization of a collection in C#

查看:209
本文介绍了在C#中的集合XML序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个类,如下所示:

 公共类信息
{
[XmlAttribute]公开字符串的语言;
公众诠释的版本;
公共图书的书;

公共信息(){}

公共信息(串升,INT V,串作者,诠释数量,诠释价格)
{
本。语言= 1;
this.version = V;
=书书新(作者,数量,价格);
}


}

公共类图书
{
[XmlAttribute]公共字符串作者;
公众诠释数量;
公众诠释价格;
[XmlIgnore]公众诠释总额;
公众的NameValueCollection nvcollection =新的NameValueCollection();

公共图书(){}

公共图书(字符串作者,诠释数量,诠释价格)
{
this.author =作者;
this.quantity =数量;
this.price =价格;
=总数量*价格;
nvcollection.Add(作者,price.ToString());
}

}



我已经创建了一个ArrayList这将增加信息类的两个实例如下:

 的FileStream FS =新的FileStream(SerializedInfo.XML,FileMode.Create); 

名单,LT;信息> arrList =新的List<信息>();

XmlSerializer的XS =新的XmlSerializer(typeof运算(列表<信息>));

信息pObj =新的信息(ABC,3,DEF,2,6);

信息pObj1 =新的信息(GHI; 4,JKL,2,8);

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

xs.Serialize(FS,arrList);

fs.Close();



但是当我尝试序列化,我得到一个例外,因为有反映类型错误系统.Collections.Generic.List`1 [ConsoleApplicationSerialization.Info]'。



谁能帮我呢?



此外,而不是NameValueCollection中,结构型我可以使用哪些?


解决方案

< STRONG> NameValueCollection中不直接
贯彻落实的ICollection 接口。
相反,的NameValueCollection 扩展
访问NameObjectCollectionBase 。这种
实现的的ICollection 接口,
和重载的添加(system.string)
方法未在$ B $实施b 的NameValueCollection 类。当你
使用的XMLSerializer 中,
的XmlSerializer 尝试序列或
反序列化的NameValueCollection
A通用的的ICollection 。因此,
查找默认
添加(System.String)。在没有
添加(system.String)方式时,
异常。




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



http://nayyeri.net/serialize-namevaluecollection



不过,我不能确定你实际上是试图实现。会是什么nvcollection包含除了书的作者,价格,一次?



你打算在书的水平使用它,或者更高的对象层次?



而不是使用一个NameValueCollection中,你可能想要一个解释,因为它有什么它可以包含条款更具灵活性:的 http://johnwsaundersiii.spaces.live.com/blog/cns!600A2BE4A82EA0A6!699.entry


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

Can anyone help me with it?

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

解决方案

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

这篇关于在C#中的集合XML序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆