Implementing IXmlSerializable requires the collection property to have a setter

I have a collection property that has its own type, which inherits from BindingList. This property is currently being serialized through the XmlSerializer, although it does not have a Setter. Now I'm trying to implement IXmlSerializable in this custom collection class and see that the WriteXml () and ReadXml () interface methods are only called if my collection property has a Setter. Why serialization ignores this property now, if there is no Setter, if before its serialization it is correct without it.

Playback:

First you have a class called "Item":

public class Item
{
    public Item() {}

    // Generates some random data for the collection
    private MyCollectionType GenerateContent()
    {
        Random ranGen = new Random();
        MyCollectionType collection = new MyCollectionType();

        for (int i = 0; i < 5; i ++)
        {
            collection.Add("Item" + ranGen.Next(0,101));
        }

        return collection;
    }

    public MyCollectionType Items
    {
        get
        {
            if (m_Items == null)
            {
                m_Items = GenerateContent();
            }
            return m_Items;
        }
    }
    private MyCollectionType m_Items = null;
}

Then create a collection of class "MyCollectionType" (note that at the beginning of the fragment, the IXMLSerializable fragment is intentionally missing):

public class MyCollectionType : BindingList<string>
{
    public MyCollectionType()
    {
        this.ListChanged += MyCollectionType_ListChanged;
    }

    void MyCollectionType_ListChanged(object sender, ListChangedEventArgs e){ }

    public MyCollectionType(ICollection<string> p)
    {
        this.ListChanged  += MyCollectionType_ListChanged;
    }

    #region Implementation of IXmlSerializable

    public void WriteXml(XmlWriter writer)
    {
        throw new NotImplementedException();
    }

    public void ReadXml(XmlReader reader)
    {
        throw new NotImplementedException();
    }

    public XmlSchema GetSchema() { return null; }

    #endregion
}

, Main() "Item":

        Item myItem = new Item();
        Item newItem = null;

        // Define an XmlSerializer
        XmlSerializer ser = new XmlSerializer(typeof(Item));

        // Serialize the Object
        using (TextWriter writer = File.CreateText(@"c:\temporary\exportedfromtest.xml"))
        {
            ser.Serialize(writer,myItem);
        }

        // Deserialize it
        using (Stream reader = new FileStream(@"c:\temporary\exportedfromtest.xml", FileMode.Open, FileAccess.Read, FileShare.Read))
        {
            using (XmlDictionaryReader xmlDictionaryReader = XmlDictionaryReader.CreateTextReader(reader, XmlDictionaryReaderQuotas.Max))
            {
                newItem = (Item)ser.Deserialize(xmlDictionaryReader);
            }
        }

, , , Setter. "IXmlSerializable" , . , "IXmlSerializable" MyCollectionType , , , WriteXml() ReadXml() . , Setter, .

+2
1

. , , XML-:

XML , , ( ). , , , DataContractSerializer XML.

, get-only - . Microsoft? .

, :

XML get-only ( , ).

(, , , , . . XML . Item, .)

? , IXmlSerializable, ; . , . , , /; XmlSerializer , , , .

+2

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


All Articles