0

Since XmlSerializer can not serialize any other properties when the class is inherited from List <>, I try to solve them with the DataContractSerializer. This should work, as described here: When a class is inherited from List<>, XmlSerializer doesn't serialize other attributes

But I get the same results. If the object is inherited from List <> the TestValue property is not serialized.

using System.Runtime.Serialization;

[Serializable]
public class XSerBase 
{
    [DataMember]
    public XSerTest XSerTest { get; set; } = new XSerTest();
}

[Serializable]
public class XSerTest : List<string>
{
    [DataMember]
    public string TestValue { get; set; }
}

{// my serialize / deserialize example

    XSerBase objectSource = new XSerBase();
    objectSource.XSerTest.TestValue = "QWERT";

    MemoryStream mem = new MemoryStream();
    DataContractSerializer dcsSource = new DataContractSerializer(typeof(XSerBase));
    dcsSource.WriteObject(mem, objectSource);
    mem.Position = 0;

    XSerBase objectDestination = null;
    DataContractSerializer dcsDestination = new DataContractSerializer(typeof(XSerBase));
    objectDestination = (dcsDestination.ReadObject(mem) as XSerBase);

    // objectDestination.XSerTest.TestValue is null
    // objectDestination.XSerTest.TestValue is "QWERT", when XSerTest is not inherited from List<string>

}

Am I missing an attribute?

marsh-wiggle
  • 1,693
  • 2
  • 23
  • 41
  • 1
    The root level of an xml file cannot be an array. Simplest way of fixing is to add a new class with the class XSerTest as a public property. Then serialize the new top level class. – jdweng Mar 22 '19 at 14:43
  • 1
    @jdweng didn't change the behavoiur. I updatet my question – marsh-wiggle Mar 22 '19 at 14:54
  • You could also apply the `CollectionDataContractAttribute` to your test class but I suspect that - as with the `XmlSerializer` - the `DataContractSerializer` does not support serializing an object that is both a collection and has added properties. See also [this answer](https://stackoverflow.com/a/4105878/1145403). – Lennart Stoop Mar 22 '19 at 14:57
  • @LennartStoop I found this: https://stackoverflow.com/a/5069266/1574221, or am I misunderstanding it? – marsh-wiggle Mar 22 '19 at 15:02

1 Answers1

1

I tried to get an inherited class List to work and was not successful. This is the best I could do

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Xml;
using System.Xml.Serialization;

namespace ConsoleApplication106
{

    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XSerBase test = new XSerBase()
            {
                XSerTest = new List<XSerTest>() { 
                    new XSerTest() { TestValue = "123"},
                    new XSerTest() { TestValue = "456"}
                }
            };


            XmlSerializer serializer = new XmlSerializer(typeof(XSerBase));

            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            XmlWriter writer = XmlWriter.Create(FILENAME,settings);

            serializer.Serialize(writer, test);

            writer.Flush();
            writer.Close();

        }

    }
    public class XSerBase
    {
        [XmlElement("XSerTest")]
        public List<XSerTest> XSerTest { get; set; }
    }
    public class XSerTest
    {
        public string TestValue { get; set; } 
    }

}
jdweng
  • 28,546
  • 2
  • 13
  • 18