Serializing class inherited from List using DataContractSerializer does not serialize object properties Unicorn Meta Zoo #1: Why another podcast? Announcing the arrival of Valued Associate #679: Cesar Manara Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!When a class is inherited from List<>, XmlSerializer doesn't serialize other attributesSerializing object that inherits from List<T>DataContractSerializer not Serializing member of class that inherits ISerializableHow can I ignore a property when serializing using the DataContractSerializer?C# - what attributes to use to support serializing using both XMLSerializer and DataContractSerializer?How to Sort a List<T> by a property in the object“Type not expected”, using DataContractSerializer - but it's just a simple class, no funny stuff?How can I change property names when serializing with Json.net?DataContractSerializer not serializing one propertyDataContractSerializer deserialize repeated property references as null in a list of objectsDataContractSerializer will not serialize protected propertiesWhy not inherit from List<T>?

Is Bran literally the world's memory?

How to get even lighting when using flash for group photos near wall?

Are these square matrices always diagonalisable?

Mistake in years of experience in resume?

Where did Arya get these scars?

Suing a Police Officer Instead of the Police Department

My admission is revoked after accepting the admission offer

France's Public Holidays' Puzzle

Israeli soda type drink

Justification for leaving new position after a short time

How would this chord from "Rocket Man" be analyzed?

"Whatever a Russian does, they end up making the Kalashnikov gun"? Are there any similar proverbs in English?

What is it called when you ride around on your front wheel?

Why did C use the -> operator instead of reusing the . operator?

What do you call the part of a novel that is not dialog?

Does Mathematica have an implementation of the Poisson Binomial Distribution?

Do I need to protect SFP ports and optics from dust/contaminants? If so, how?

Split coins into combinations of different denominations

Are there moral objections to a life motivated purely by money? How to sway a person from this lifestyle?

Error: Syntax error. Missing ')' for CASE Statement

Check if a string is entirely made of the same substring

Putting Ant-Man on house arrest

The art of proof summarizing. Are there known rules, or is it a purely common sense matter?

With indentation set to `0em`, when using a line break, there is still an indentation of a size of a space



Serializing class inherited from List using DataContractSerializer does not serialize object properties



Unicorn Meta Zoo #1: Why another podcast?
Announcing the arrival of Valued Associate #679: Cesar Manara
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!When a class is inherited from List<>, XmlSerializer doesn't serialize other attributesSerializing object that inherits from List<T>DataContractSerializer not Serializing member of class that inherits ISerializableHow can I ignore a property when serializing using the DataContractSerializer?C# - what attributes to use to support serializing using both XMLSerializer and DataContractSerializer?How to Sort a List<T> by a property in the object“Type not expected”, using DataContractSerializer - but it's just a simple class, no funny stuff?How can I change property names when serializing with Json.net?DataContractSerializer not serializing one propertyDataContractSerializer deserialize repeated property references as null in a list of objectsDataContractSerializer will not serialize protected propertiesWhy not inherit from List<T>?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








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?










share|improve this question



















  • 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 at 14:43






  • 1





    @jdweng didn't change the behavoiur. I updatet my question

    – marsh-wiggle
    Mar 22 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.

    – Lennart Stoop
    Mar 22 at 14:57












  • @LennartStoop I found this: stackoverflow.com/a/5069266/1574221, or am I misunderstanding it?

    – marsh-wiggle
    Mar 22 at 15:02


















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?










share|improve this question



















  • 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 at 14:43






  • 1





    @jdweng didn't change the behavoiur. I updatet my question

    – marsh-wiggle
    Mar 22 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.

    – Lennart Stoop
    Mar 22 at 14:57












  • @LennartStoop I found this: stackoverflow.com/a/5069266/1574221, or am I misunderstanding it?

    – marsh-wiggle
    Mar 22 at 15:02














0












0








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?










share|improve this question
















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?







c# xml serialization xml-serialization datacontractserializer






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 22 at 15:02







marsh-wiggle

















asked Mar 22 at 14:29









marsh-wigglemarsh-wiggle

68021025




68021025







  • 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 at 14:43






  • 1





    @jdweng didn't change the behavoiur. I updatet my question

    – marsh-wiggle
    Mar 22 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.

    – Lennart Stoop
    Mar 22 at 14:57












  • @LennartStoop I found this: stackoverflow.com/a/5069266/1574221, or am I misunderstanding it?

    – marsh-wiggle
    Mar 22 at 15:02













  • 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 at 14:43






  • 1





    @jdweng didn't change the behavoiur. I updatet my question

    – marsh-wiggle
    Mar 22 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.

    – Lennart Stoop
    Mar 22 at 14:57












  • @LennartStoop I found this: stackoverflow.com/a/5069266/1574221, or am I misunderstanding it?

    – marsh-wiggle
    Mar 22 at 15:02








1




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 at 14:43





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 at 14:43




1




1





@jdweng didn't change the behavoiur. I updatet my question

– marsh-wiggle
Mar 22 at 14:54





@jdweng didn't change the behavoiur. I updatet my question

– marsh-wiggle
Mar 22 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.

– Lennart Stoop
Mar 22 at 14:57






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.

– Lennart Stoop
Mar 22 at 14:57














@LennartStoop I found this: stackoverflow.com/a/5069266/1574221, or am I misunderstanding it?

– marsh-wiggle
Mar 22 at 15:02






@LennartStoop I found this: stackoverflow.com/a/5069266/1574221, or am I misunderstanding it?

– marsh-wiggle
Mar 22 at 15:02













1 Answer
1






active

oldest

votes


















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:temptest.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;








share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55301839%2fserializing-class-inherited-from-list-using-datacontractserializer-does-not-se%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    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:temptest.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;








    share|improve this answer



























      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:temptest.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;








      share|improve this answer

























        1












        1








        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:temptest.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;








        share|improve this answer













        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:temptest.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;









        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 22 at 15:51









        jdwengjdweng

        18.4k2917




        18.4k2917





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55301839%2fserializing-class-inherited-from-list-using-datacontractserializer-does-not-se%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현