Can LinkedList serialize?

Here are my classes: http://pastebin.com/3dc5Vb1t

When i try to run

BookStore b = new BookStore();
b.LoadFromXML(Server.MapPath("list.xml"));
Label1.Text = b.ToString();

I get the following error:

You must implement the default accessory in System.Collections.Generic.LinkedList`1 [[Book, App_Code.cxsacizw, Version = 0.0.0.0, Culture = neutral, PublicKeyToken = null]] because it inherits from ICollection.

Source of error XmlSerializer s = new XmlSerializer(typeof(BookStore));

When I tried to find a solution on google, I found that LinkedList has some serialization problems. How can I handle this?

Many thanks.

+3
source share
3 answers
+3

, .

: linkedlist-t-can-not-be-serialized-using-the-xmlserializer. :

Microsoft 11/11/2004 19:35
LinkedList . LinkedList XMLSeriliazable.

+5

:

.Net, , - () .., . , -, : , , (, ), .Net , , , . (, - XML), .

: ? , , , . , , , ( ) .

,.Net , de/serialization. -, System.SerializableAttribute(http://msdn.microsoft.com/en-us/library/system.serializableattribute.aspx). System.Runtime.Serialization.ISerializable(http://msdn.microsoft.com/en-us/library/system.runtime.serialization.iserializable.aspx), . , , - SerializationInfo, GetObjectData (...) (, "" ) . , , , .

. XML, System.Xml.Serialization.IXmlSerializable. , , ; ( ). , XML.Net; , - , ;).

: LinkedList, "" . ; .

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.Xml;
using System.IO;

namespace WFTest {
    [Serializable]
    class SerializableLinkedList<T>: LinkedList<T>, ISerializable, IXmlSerializable {
        void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) {
            info.AddValue("value", this.ToList());
        }
        // Implied by ISerializable, but interfaces can't actually define constructors:
        SerializableLinkedList(SerializationInfo info, StreamingContext context)
            : base((IEnumerable<T>)info.GetValue("value", typeof(List<T>))) { }

        System.Xml.Schema.XmlSchema IXmlSerializable.GetSchema() { return null; }

        void IXmlSerializable.ReadXml(XmlReader reader) {
            this.Clear(); // Start with an empty list
            reader.ReadStartElement(); // Skips the opening tag
            while (reader.LocalName=="item") { // Retrieve all elements:
                T value;
                if(reader.IsEmptyElement) { // If element is empty...
                    value=default(T); // the item value falls back to default(T)
                    reader.ReadStartElement(); // and consume the (empty) element
                } else {
                    // IIRC, ReadInnerXml() consumes the outer tag, despite not returning them.
                    value=(T)((new XmlSerializer(typeof(T))).Deserialize(new StringReader(reader.ReadInnerXml())));
                }
                this.AddLast(value);
            }
            reader.ReadEndElement(); // Consumes the remaining closing tag from the reader
        }

        void IXmlSerializable.WriteXml(XmlWriter writer) {
            foreach(T item in this) {
                // Format the item itself:
                StringBuilder sb=new StringBuilder();
                (new XmlSerializer(typeof(T))).Serialize(XmlWriter.Create(sb), item);
                XmlDocument doc=new XmlDocument();
                doc.LoadXml(sb.ToString());
                // and now write it to the stream within <item>...</item> tags
                writer.WriteStartElement("item");
                writer.WriteRaw(doc.DocumentElement.OuterXml);
                writer.WriteEndElement(); // </item>
            }
        }
    }
}

"" LinkedList ( , LinkedList), . , , , "T" , , .

, : , , , - (, , ). , .

, , StringBuilder ToString(): , + = String, ( ). , , .

,

+4
source

Source: https://habr.com/ru/post/1737816/


All Articles