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

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript